BiruLyu
5/25/2017 - 10:23 PM

7. Reverse Integer.java

/*
For python: 
-123 % 10 = 7
-123 / 10 = -13

For Java:
-123 % 10 = -3
-123 / 10 = -12
*/

public class Solution {
    public int reverse(int x) {
        int res = 0;
        while( x != 0){
            int tail = x % 10;
            int temp = res * 10 + tail;
            if( (temp - tail) / 10 != res) return 0;//overflow
            res = temp;
            x /= 10;
        }
        return res;
    }
}

/*
0
123
-123
1000000003
-2147483648
2147483647

*/