-
Notifications
You must be signed in to change notification settings - Fork 0
/
PalindromeNum.cc
62 lines (59 loc) · 1.29 KB
/
PalindromeNum.cc
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
59
60
61
62
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
class Solution {
public:
bool isPalindrome(int x) {
if (!x) return true;
if (x < 0) return false;
x = abs(x);
int base = 1;
while (x / base >= 10) base *= 10; // !!! use '>=' not '>'
int base1 = 1;
while (base1 <= base) {
int right = x / base1;
right %= 10;
int left = x / base;
left %= 10;
if (left != right) return false;
base1 *= 10;
base /= 10;
}
return true;
}
/*bool isPalindrome(int x) {
if (x < 0)
return false;
if ((x / 10) == 0)
return true;
int a = x;
int div = 1;
int count = 0;
while (a / div && div * 10 > div) {
div *= 10;
//cout << "div " << div << endl;
count++;
}
cout << count << endl;
int i = 0;
while (i < (count / 2)) {
int left = (a / int(pow(10, count - i - 1))) % 10;
int right = (a / int(pow(10, i))) % 10;
cout << "left:" << left << "-right:" << right << endl;
if (left == right)
i++;
else
return false;
}
return true;
}*/
};
int main(int argc, char **argv) {
Solution sol;
if (sol.isPalindrome(2147447412))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}