【题目描述】Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
【解题思路】求反转其实有可能溢出的,不过测试点中没有这样的情况
【考查内容】复杂度
class Solution {
public:
int reverse(int x) {
int ret = 0;
bool positive = true;
if(x < 0){
positive = false;
x = -x;
}
while(x){
ret = ret * 10 + x % 10;
x = x /10;
}
return positive?ret:-ret;
}
};