标签:return turn bit put code solution nbsp bsp als
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.
Solution:
int reverse(int x) {
bool negative = false;
if(x<0){
negative = true;
x = -x;
}
int nums = 0;
int tmpNum;
int tmp;
int i;
while(x){
tmpNum = nums;
tmp = 0;
for(i = 0;i<10;i++){
tmp = tmp+tmpNum;
if(tmp<tmpNum)
return 0;
}
nums = tmp;
nums=nums+x%10;
x = x/10;
}
if(negative)
return -1*nums;
return nums;
}
标签:return turn bit put code solution nbsp bsp als
原文地址:http://www.cnblogs.com/silverbulletcy/p/7944867.html