Friday, September 22, 2017

7. Reverse Integer

https://leetcode.com/problems/reverse-integer/description/
    int reverse(int x) {
        if(x == INT_MIN) return 0;
        if(x < 0) return -reverse(-x);
        int res = 0;
        while(x) {
            int r = x%10;
            if(res > INT_MAX/10) return 0;
            res = res * 10 + r;
            x /= 10;
        }
        return res;
    }

No comments:

Post a Comment