Solution in Java


  • 0
    S
    public int reverse(int x) {
            int rem = 0;
            String op = "";
            boolean neg = (x < 0)? true: false;
            if(x == Integer.MIN_VALUE)
                return 0;
            x = neg ? -x : x;
            
            while( x >= 10){
                rem = x % 10;
                x = x / 10;
                op = op + rem;
            }
            op = op + x;
            try{
                return ((neg) ? -Integer.parseInt(op): Integer.parseInt(op));
            }catch(Exception ex){
                return 0;
            }
        }
    

Log in to reply
 

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