Solution in Java


  • 0
    D

    //Make use of Integer.MAX_VALUE
    long p = x;
    boolean isNegtive = false;
    if(p < 0){
    p = 0 - p;
    isNegtive = true;
    }

        long answer = 0;
        while(p != 0){
        	answer *= 10;
        	answer += p % 10;
            p /= 10;
        }
    
        if(answer > Integer.MAX_VALUE) return 0;
        return isNegtive  ? 0 - ((int)answer) : (int)answer;

Log in to reply
 

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