您的位置:首页 > 理论基础 > 数据结构算法

LeetCode-7-Reverse Integer-E

2018-02-03 03:16 281 查看
Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123

Output: 321

Example 2:

Input: -123

Output: -321

Example 3:

Input: 120

Output: 21

Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

代码如下:

(在long long那里的数据类型的地方遇到了一点挫折)

int reverse(int x) {
int i=x,bit=0;
long long result=0;
vector<int>number;
while(i!=0){
number.push_back(i%10);
i=i/10;
bit++;
}
for(int i=0;i<number.size();i++){
result+=number[i]*pow(10,bit-1);
bit--;
}
return (result<INT_MIN || result>INT_MAX) ? 0 : result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构与算法