5 lines, 2ms Java solution


  • 12
    I
    public class Solution {
        public int reverse(int x) {
            long answer = 0;
            while(x != 0) {
                answer = 10 * answer + x % 10;
                x /= 10;
            }
            return (answer > Integer.MAX_VALUE || answer < Integer.MIN_VALUE) ? 0 : (int) answer;
        }
    }

  • 0
    Y

    I think it's a good idea.


Log in to reply
 

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