Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
Subscribe to see which companies asked this question.
简单题
class Solution {
public:
int reverse(int x) {
string s,re;
s=to_string(x);
if(s[0]=='-') s.erase(0,1);
for(auto x:s)
re=x+re;
if(x>0) return stoll(re)>INT32_MAX?0:stoll(re);
else return stoll(re)>INT32_MAX?0:-1*stoll(re);
}
};