簡直是賣萌的...
解決方法:
int reverse(int x) {
int res = 0;
while(x){
if(abs(res) > INT_MAX/10) return 0;
res = res*10+x%10;
x/=10;
}
return res;
}
不過這個有點慢 31ms
下面是8ms的版本摹菠,不過沒啥用善茎,之所以快是因為加快了輸入輸出的速度。
static int x = []() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
static int pr = []() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
int reverse(int x) {
int result = 0;
while (x)
{
pr = result * 10 + x % 10;
if (result != pr / 10) return 0;
result = pr;
x /= 10;
}
return result;
}
};