Reverse Integer solution in java


  • 0
    W
        public int reverse(int x){
            Integer retVal = 0;
            if(x == 0){
                return 0;
            }
            String str = x>0?Integer.valueOf(x).toString():Integer.valueOf(x).toString().substring(1);
            try{
                retVal = Integer.valueOf(new StringBuilder(str).reverse().toString());
            }catch(Exception ex){}
    
            return x>=0?retVal:-retVal;
        }
    

Log in to reply
 

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