PU Palindrome Number

Jan 01, 1970

Determine whether an integer is a palindrome. Do this without extra space.

C Solution:

bool isPalindrome(int x) {
    if (x < 0) return false;
    if (x && x % 10 == 0) return false;
    int y = 0;
    while (x > y) {
        y = y * 10 + x % 10;
        x /= 10;
    }
    return x == y || x == y / 10;
}

Summary:

  1. When x < 0, return false..., this should be clarified.
  2. The only special case is when the least-significant digit is 0. Remember.

LeetCode: 9. Palindrome Number