Reverse Integer in C


  • 0
    Y
    int reverse(int x) {
        int a = 1 << 31;
        int neg = 0;
        if (x < 0){
            neg = 1;
            x = -x;
        }
        double res = 0;
        while (x != 0){
            res = res*10 + x%10;
            x /= 10;
        }
        if (res > a - 1 || res < -a){
            return 0;
        }
        res = (int) res;
        if (neg){
            res = -res;
        }
        return res;
    }
    

Log in to reply
 

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