-
Notifications
You must be signed in to change notification settings - Fork 93
/
9.Palindrome-Number.java
66 lines (58 loc) · 1.39 KB
/
9.Palindrome-Number.java
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
63
64
65
66
class Solution {
public boolean isPalindrome(int x) {
if (x < 0)
return false;
int temp = x;
int len = 0;
while (temp != 0) {
temp /= 10;
len ++;
}
temp = x;
int left, right;
for (int i = 0; i < len / 2; i++) {
right = temp % 10;
left = temp / (int) Math.pow(10, len - 2 * i - 1);
left = left % 10;
if (left != right)
return false;
temp /= 10;
}
return true;
}
public boolean isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while ( x / div >= 10) {
div *= 10;
}
while (x !=0) {
int l = x / div;
int r = x % 10;
if (l != r) return false;
// Remove left and right number
x = (x % div) / 10;
div /= 100;
}
return true;
}
}
class Solution {
public boolean isPalindrome(int x) {
int r,s=0,number=x;
if(number<0){
return false;
}
while (number!=0){
r=number%10;
s= s*10 +r;
number/=10;
}
if (s==x){
return true;
}
else {
return false;
}
}
}