来源:Leetcode Problem Set - Algorithm Problem7
题目描述:Given a 32-bit signed integer, reverse digits of an integer.
解题思路:先判断数字的正负,然后判断数字,按位逐个读取数字,使用除以10得到每一位的数字
代码解答:
class Solution {
public:
int reverse(int x) {
bool nag = false;
long int back = 0;
if (x < 0) {
nag = true;
x = -x;
}
while(x > 0) {
back *= 10;
if(back > 2147483648) return 0;
back += (x%10);
x /= 10;
}
int Back = back;
if (nag) Back = 0-back;
return Back;
}
};