Java Reverse Integer


  • 0
    S
    class Solution {
        public int reverse(int x) {
         if(x<=Integer.MIN_VALUE||x>=Integer.MAX_VALUE){
               // throw new IllegalArgumentException("Num is Wrong!");
                 return 0;
            }
            long res=0 ;
            int flags = x < 0 ? -1 : 1;
            x = Math.abs(x);
            while(x > 0){
                   res = res*10+x%10;
                   x/=10;
    
            }
            if(res<=Integer.MIN_VALUE||res>=Integer.MAX_VALUE){
                //throw new IllegalArgumentException("Num is Wrong!");
                return 0;
            }
            int r = (int)res*flags;
            return r;
    }
    }
    

    perfect


Log in to reply
 

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