# Leetcode: Palindrome Number

By | 2016 年 5 月 29 日

# Problem

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

# Analyze

This is the previous post on this question
This is easy question, just find the most left digit and then compare each digit from left-right ends to center.

# Code

## C++ code accept by Leetcode, which exclude negative number like -121 as palindrome

```class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
int highDigit = 1;
while(x / highDigit >= 10)
highDigit *= 10;
int lowDigit = 1;
while(highDigit > lowDigit){
int high = (x / highDigit) % 10;
int low = (x/lowDigit) % 10;
if(high != low)
return false;
highDigit /= 10;
lowDigit *= 10;
}
return true;
}
};
```

## C++ code for who consider negative number like -121 also as palindrome

```class Solution {
public:
bool isPalindrome(int x) {
int highDigit = 1;
while(x / highDigit >= 10 || x / highDigit <= -10)
highDigit *= 10;
int lowDigit = 1;
while(highDigit > lowDigit){
int high = (x / highDigit) % 10;
int low = (x/lowDigit) % 10;
if(high != low)
return false;
highDigit /= 10;
lowDigit *= 10;
}
return true;
}
};
```