# [LeetCode]9.Palindrome Number

#### 【题目】

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.

#### 【代码】

/*********************************
*   日期：2015-01-20
*   作者：SJF0115
*   题目: 9.Palindrome Number
*   网址：https://oj.leetcode.com/problems/palindrome-number/
*   结果：AC
*   来源：LeetCode
*   博客：
**********************************/
#include <iostream>
using namespace std;

class Solution {
public:
bool isPalindrome(int x) {
// negative integer
if(x < 0){
return false;
}//if
int tmp = x;
int n = 0;
// reverse an integer
while(tmp){
n = n * 10 + tmp % 10;
tmp /= 10;
}//while
return (x == n);
}
};

int main(){
Solution solution;
int num = 1234321;
bool result = solution.isPalindrome(num);
// 输出
cout<<result<<endl;
return 0;
}


#### 【代码二】

class Solution {
public:
bool isPalindrome(int x) {
// negative integer
if(x < 0){
return false;
}//if
// 位数
int divisor = 1;
while(x / divisor >= 10){
divisor *= 10;
}//while
int first,last;
while(x){
first = x / divisor;
last = x % 10;
// 高位和低位比较 是否相等
if(first != last){
return false;
}//if
// 去掉一个最高位和一个最低位
x = x % divisor / 10;
divisor /= 100;
}//while
return true;
}
};

02-09 529

04-05 365
07-21 1053
08-30 628
01-27 2912
02-28 176
03-01 2524
12-05 2903
05-31 155
10-31 82
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客