C++ Solution


  • 0
    B
    int reverseFinal(int x){
        bool isNeg = false;
        int result = 0;
        int c = 2147483647;
        unsigned int d = 0;
        
        if(x<0){
            isNeg = true;
            x = x*-1;
        }
        
        
        while(x){
            if(d > (c/10))
                return 0;
            
            d = d*10 + x%10;
            x /= 10;
        }
        
        
        if(d>c)
            return 0;
        
        result = d;
        
        if(isNeg)
            result *= -1;
        
        return result;
    }
    
    int reverse(int x) {
        int result = reverseFinal(x);
        
        return result;
    }

Log in to reply
 

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