Simple C++ Solution 16ms


  • 0
    X
    class Solution {
    public:
        int reverse(int x) {
            int x_cp = abs(x);
            long result = 0;
            while(x_cp) {
                result = result*10+ x_cp%10;
                x_cp /= 10;
            }
            if(result > 2147483647 || result < -2147483648) return 0;
            return x >0 ? result : -result;
        }
    };

  • 0
    M

    When the x==-2147483648,the x_cp will overflow.


Log in to reply
 

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