Share my simple solution using a long value


  • 0
    E
    public class Solution {
    public int reverse(int x) {
        if(x==0 || x==Integer.MIN_VALUE) return 0;
        boolean positive = x>0;
        if(!positive){
            x *= -1;
        }
        long result = 0;
        while(x>0){
            result = result*10 + x%10;
            x /=10;
        }
        if(positive){
            return (result>Integer.MAX_VALUE)?0:(int)result;
        }else{
            result *= -1;
            return (result<Integer.MIN_VALUE)?0:(int)result;
        }
    }
    

    }


Log in to reply
 

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