Solution in JAVA


  • 0
    Y
    class Solution {
        public int reverse(int x) {
            int count = 1;     
            boolean flag = true;
            if(x == Integer.MIN_VALUE)
                return 0;
            if(x < 0){
                x = Math.abs(x);
                flag = false;
            }
            int y = x;
            while(y / 10 > 0){
                count ++;
                y /= 10;
            }
            int[] arr = new int[count];
            for(int i = 1;i <= arr.length ;i ++){
                arr[i - 1] = x % 10;
                x /= 10;
            }
            long reverseInt = 0l;
            for(int i = 0;i < arr.length;i ++){
                reverseInt *= 10;
                reverseInt += arr[i];
                
                        
            }
            if(reverseInt > Integer.MAX_VALUE)
                return 0;
            int reverse = (int)reverseInt;
            return flag ? reverse :(-1) * reverse ;
        }
    }
    

Log in to reply
 

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