-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome Number.h
More file actions
40 lines (35 loc) · 861 Bytes
/
Palindrome Number.h
File metadata and controls
40 lines (35 loc) · 861 Bytes
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
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
int div = 1;
int tmp = x;
while(tmp >= 10) {
div *= 10;
tmp /= 10;
}
while(x) { // while(x > 10) is wrong condition, case 100021
int l = x/div;
int r = x%10;
if(l != r) return false;
x = (x%div)/10;
div /= 100;
}
return true;
}
};
// possibly wrong solution, could overflow, case: 2147483647
public class Solution {
public boolean isPalindrome(int x) {
if(x < 0)
return false;
int x1 = x;
int x2 = 0;
while(x1/10 > 0) {
x2 = x2*10 + x1%10;
x1 = x1/10;
}
x2 = x2*10 + x1;
return (x == x2);
}
}