Accepted 16ms Solution in C


  • 0
    C
    int reverse(int x) {
        int new = 0;
        int rem;
        if(x >= INT_MAX || x <= INT_MIN) return 0;
        while(x != 0) {
            if(new > INT_MAX/10 || new < INT_MIN/10) return 0; // if x is out of boudary and will cause overflow
            rem = x%10;
            new = 10*new;
            new = new + rem;
            x = (x-rem)/10;
            
        }
        
        
        return new;
    }
    

Log in to reply
 

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