Solution in Java


  • 0
    L

    The solution is very straightforward. We get the digits on each position by using mod operation.
    The time complexity is O(1). The space complexity is also O(1).

    class Solution {
        public int reverse(int x) {
            long reverse = 0;
            int left = x;
            while(left != 0){
                reverse = reverse * 10 + left % 10;
                left = left / 10;
            }
            if(reverse < Integer.MIN_VALUE || reverse > Integer.MAX_VALUE){
                return 0;
            }else{
                return (int)reverse;
            }
        }
    }
    
    

Log in to reply
 

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