# 【leetcode】Palindrome Number

Question：

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

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

Anwser 1：

```class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x < 0) return false;

int div = 1;
while(x/10 >= div){     // get large division
div *= 10;
}

while(x > 9){
int high = x / div;     // left digit
int low = x % 10;       // right digit

if(high != low){
return false;
}

x = (x % div) / 10;     // get number between first and last
div /= 100;
}

return true;
}
};```

Anwser 2：

```class Solution {
public:
bool check(int x, int &y) {
if (x == 0) return true;

if (check(x/10, y) && (x%10 == y%10)) {
y /= 10;
return true;
} else {
return false;
}
}

bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x < 0) return false;

return check(x, x);
}
};```

0 条评论

• ### 【leetcode】Valid Number

Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" ...

• ### 【leetcode】Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric c...

• ### 1082 与7无关的数(思维题,巨坑)

1082 与7无关的数 题目来源：                 有道难题 基准时间限制：1 秒 空间限制：131072 KB 分值: 5        ...

• ### LeetCode 50. Pow(x, n)（二分查找）

示例 输入: 2.00000, -2 输出: 0.25000 解释: 2-2 = 1/22 = 1/4 = 0.25 说明: -100.0 < x <...

• ### 用小程序·云开发轻松构建二手书商城小程序（上）丨实战

今天是“世界读书日”，传承知识，手有余香~本文教你用小程序·云开发轻松制作二手书交易商城小程序，让智慧延续，让温暖传递。

• ### HDU 1669 -- POJ 2289 Jamie's Contact Groups(二分+二分图多重匹配)

题目链接(HDU)：http://acm.hdu.edu.cn/showproblem.php?pid=1669

• ### O(1)最大值最小值的均值滤波算法

之前做过最大值最小值滤波基本上复杂度是非常高的，因为涉及到遍历w*h的滑动窗口中的所有值然后求出这个窗口所有值的最大和最小值。尽管可以使用sse优化，但速度仍然...

• ### VUE 入门基础(6)

六，条件渲染 　　v-if 添加一个条件块 　　　　<h1 v-if=“ok”>Yes</h1> 　　也可以用v-else 添加else 块 　　<templa...