Solution in C


  • 0
    Q
    int reverse(int x) {
        int limit1 = pow(2, 31) - 1;
        int limit2 = - pow(2, 31);
        int num[10];
        
        int i;
        for(i=0; i<10; i++)
            num[i] = 0;
        
        if(x>limit1 || x<limit2)
            return 0;
        
        i = 0;
        while(abs(x)>=1 && i<10) {
            num[i++] = x % 10;
            x = x / 10;
        }
        
        int res = 0, tem = 0;
        for(i=9; i>=0; i--){
            if(num[i] != 0)
                break;
        }
        
        while(i >= 0) {
            res += num[i--] * pow(10, tem++);
        }
        
        if(res==limit1 || res==limit2)
            return 0;
        else
            return res;
    }
    

Log in to reply
 

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