Solution in Java


  • 0
    A
        public int reverse(int x) {
            if(x == 0){
                // edge case
                return 0;
            }
            long reversedNum = 0;
            long input_long = x;
            while(input_long % 10 == 0){
                input_long /= 10;
            }
            while (input_long != 0) {
                reversedNum = reversedNum * 10 + input_long % 10;
                input_long = input_long / 10;
            }
            if (reversedNum > Integer.MAX_VALUE || reversedNum < Integer.MIN_VALUE) {
                return 0;
            }
            return (int) reversedNum;
        }
    
    

Log in to reply
 

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