leetcode

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

Solution:

  1. For integer range, if negative, then can not be palindrome number.
  2. For integer range, possibly overflow, so use long

Most importantly, after the rolling x is changed, so need a copy of the original value of x.

class Solution {

public:

bool isPalindrome\(int x) {

    int original = x;

    if \(x < 0\) return false;



    long y = 0;

    while \(x > 0\) {

        int tmp = x % 10;

        y = y \* 10 + tmp;

        x = x / 10;

    }

    if \(\(y - original\) == 0\)

        return true;

    else 

        return false;

}

};

results matching ""

    No results matching ""