C solution


  • 0
    H
    int reverse(int x) {
        int i=0;
        int num = abs(x);  /*x may be negative*/
        while(num!=0){
            if(i> (INT_MAX - num%10)/10)  /*the next updated value i may overflow*/
                return 0;
            i=i*10+num%10;
            num = num/10;
        }
        i = x>0?i:-1*i;
        return i;
        
    }
    

Log in to reply
 

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