# 【LeetCode】 9. Palindrome Number
## Description
> Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
> 判斷一個數字是不是回文。回文指前唸到後和後唸到前一樣。
## Example:
```
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
```
## Solution
* 兩種做法:
* 把每一個位數拆開之後直接判斷該數字是不是回文,即第i個和第end-i是不是一樣的。概念和做字串的回文很像。
* 將數字反轉,然後檢查反轉前後是不是一樣的。小心反轉後溢位。
* 兩種方法皆注意負數不算回文。
### Code
* 方法一
```C++=1
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)return false;
vector<int> v;
while(x!=0)
{
v.push_back(x%10);
x/=10;
}
for(int i = 0;i<v.size()/2;i++)
{
if(v[i]!=v[v.size()-i-1])
return false;
}
return true;
}
};
```
* 方法二
```C++=1
class Solution {
public:
bool isPalindrome(int x) {
long long int num=0;
for(int i = x;i>0;i/=10)
{
num*=10;
num+=i%10;
}
return x==num;
}
};
```
###### tags: `LeetCode` `C++`