-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.cpp
More file actions
58 lines (52 loc) · 1.45 KB
/
9.cpp
File metadata and controls
58 lines (52 loc) · 1.45 KB
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
53
54
55
56
57
58
/*************************************************************************
> File Name: 9.cpp
> Author: Alan
> Mail: [email protected]
> Created Time: Wed 11 Nov 2015 10:13:33 AM CST
> Problem Name: Palindrome Number
> Difficulty: Easy
> Description:
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.
************************************************************************/
#include<iostream>
using namespace std;
class Solution
{
public:
bool isPalindrome(int x)
{
if(x < 0)
{
return false;
}
if(x < 10)
{
return true;
}
int tmp = x, res = 0;
while(tmp > 0)
{
res = res * 10 + tmp % 10;
tmp /= 10;
}
return res == x;
}
};
int main()
{
int num = 123454321;
Solution sol = Solution();
cout << num << " is ";
if(!sol.isPalindrome(num))
{
cout << "not ";
}
cout << "a palindrome" << endl;
}