Accepted c++ solution


  • 0
    S
    class Solution {
    public:
        int reverse(int x) {
            int sign = (x<0)?-1:1;
            x*=sign;
            int rev = 0;
            while(x != 0){
                if (rev>(numeric_limits<int>::max()-x%10)/10)
                    return 0;
                rev = rev*10 + x % 10;
                x = x/10;
            }
         
            return sign*rev;
        }
    };

  • 0
    C

    if x < 0, I think x = 0 - x is more efficient than x*=-1


Log in to reply
 

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