Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
要把int转化为long,否则会出现:
Submission Result: Wrong Answer More Details
Output:1056389759
Expected:0
要判断是否溢出这个问题,否则会出现:
Input: 1534236469
Output: 9646324351
Expected: 0
最终提交成功
class Solution { public: int reverse(int x) { long x1,y = 0; if(x<0) x1 = (-1) * x; else x1 = x; while(1){ long n = x1 % 10; //最低位 y = y * 10 + n; x1 /= 10; if(x1 == 0) break; } if(y > 2147483648 || y < -2147483648){ //判断有没有溢出 return 0; } if(x<0) y = (-1)* y; return y; } };