Saturday, October 7, 2017

7. Reverse Integer



 67.49 %
class Solution {
    public int reverse(int x) {
        // x = -123, return -321
        int result = 0;
        int currResult = 0;
        int tail = 0;
        while (x != 0) {
            tail = x % 10;
            currResult = result * 10 + tail;
            if ((currResult - tail) / 10 != result) return 0;
            result = currResult;
            x /= 10;
        }
        return result;
    }
}

No comments:

Post a Comment