# 9. Palindrome Number

## 刷题内容

• https://leetcode.com/problems/palindrome-number
• 内容描述

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

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.

Coud you solve it without converting the integer to a string?


## 解题方案

class Solution {
public:
bool isPalindrome(int x) {
long long ret = 0;
int num = x;
if(x < 0)
return false;
while(num)
{
ret = 10 * ret + num % 10;
num /= 10;
}
if(ret == x)
return true;
return false;
}
};


class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
return false;

int cnt = 0;
long fac = 1;
int div = INT_MAX;
while (div != 0) {
cnt++;
fac *= 10;
div = x/fac;
}

fac /= 10;
for (int i=0; i<cnt/2; i++) {
int last = x%10;
int first = x/fac;
if (first != last)
return false;
x = x % fac;
x = (x-last)/10;
fac /= 100;
}

return true;
}
};