Short and simple code in java for reverse integer


  • 0
    S
    class Solution {
       int y,w=0;
        public int reverse(int x) {
           if(x<0){
               x= 0-x;
               y =0-rev(x);  
         }
            else{        
            y=rev(x);
            }
        return y;
        }
            public int rev(int x){
                StringBuilder b = new StringBuilder();
               String s=b.append(x).reverse().toString();
               try{
                    w= Integer.parseInt(s);
            }
                catch(NumberFormatException ex){
                    System.out.println("NumberFormatException");
                }
       return w;
            }
    }
    

Log in to reply
 

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