Reverse the entire integer and compare

  • 0

    Compare the reversed integer with the original one.

    We don't need extra code to handle the overflow issue.

    If it doesn't overflow, we are happy. Otherwise If it does overflow, it means that the last digit is greater than the first digit. It must not be palindrome. We are happy too.

    class Solution {
        public boolean isPalindrome(int x) {
            if(x < 0) return false;
            int r = 0;
            int t = x;
            while(t != 0) {
                r = 10 * r + t % 10;
                t /= 10;
            return r == x;

Log in to reply

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