Java Solution


  • 0
    class Solution {
        public int reverse(int x) {
            boolean isNeg = false;
            if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) {
                return 0;
            }
            if(x < 0) {
                isNeg = true;
                x *= -1;
            }
            long rev = 0;
            while(x > 0) {
                rev *= 10;
                rev += x % 10;
                if(rev > Integer.MAX_VALUE){
                    return 0;
                }
                x /= 10;
            }
            return (int) (isNeg?rev * -1: rev);
            
        }
    }
    
    

Log in to reply
 

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