/*
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Example2: x = -123, return -321
*/
class Solution {
public:
int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int signal = 1;
if (x<0) signal = -1;
int reminder = x * signal;
int temp;
int result = 0;
int const MAX = 20000000;
while(reminder > 0 && result < MAX){
temp = reminder/10;
result = result*10 + reminder - temp*10;
reminder = temp;
}
if(result < MAX) return signal * result;
else return MAX;
}
};
No comments:
Post a Comment