C++ solution (16ms & 8 lines)


  • 2
    B
    class Solution {
    public:
        int reverse(int x) {
            int flag = (x < 0) ? -1 : 1;
            x *= flag;
            int res = 0;
            do {
                if (res > INT_MAX / 10 || (INT_MAX - res * 10) < x % 10) return 0;
                res = res * 10 + x % 10;
            } while (x /= 10);
            return res * flag;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.