知识点:
- 内置变量 INT_MAX INT_MIN
- 运算结果是否溢出的判断
- 判断pop>7即pop>INT_MAX%10
- 判断pop<-8即pop<INT_MIN%10
-
class Solution { public: int reverse(int x) { int pop,ans=0; while(x) { pop = x%10; x /= 10; if(ans > INT_MAX/10 || (ans == INT_MAX/10 && pop >7)) return 0; if(ans < INT_MIN/10 || (ans == INT_MIN/10 && pop <-8)) return 0; //cout << "pop: " << pop << " ans: " << ans << ' '; ans = ans*10 + pop; } return ans; } };