leetcode-9-palindrome number

题目

判断整数是否是回文数

分析

特殊情况:负数,被10整除的数

C++代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

* 字符串反转判断
class Solution {
public:
bool isPalindrome(int x) {
stringstream ss;
string s;
ss<<x;ss>>s;ss.clear();
string sl=s;
reverse(s.begin(),s.end());
return sl == s;

}
};
*/



* 整数反转,判断结果与之前是否相等
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
else{
int n = 0;
int m = x;
while(m!=0){
n=n*10+m%10;
m/=10;
}
return n ==x;
}
}
};

*/


* 判断整数的后一半和前一半是否相等
*/

class Solution {
public:
bool (int x) {
if(x<0|| (x!=0 &&x%10==0)) return false;
int sum=0;
while(x>sum)
{
sum = sum*10+x%10;
x = x/10;
}
return (x==sum)||(x==sum/10);
}
};