-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPalindromeNumber.java
More file actions
59 lines (55 loc) · 1.36 KB
/
PalindromeNumber.java
File metadata and controls
59 lines (55 loc) · 1.36 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
59
package findpattern.palindromic;
// Source : https://leetcode.com/problems/palindrome-number/
// Id : 9
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-09-25
// Topic : Math
// Level : Easy
// Other :
// Tips :
// Result : 100.00% 5.02%
public class PalindromeNumber {
// 6ms
public boolean isPalindrome(int x) {
if (x == 0)
return true;
if (x < 0 || x % 10 == 0)
return false;
int tmp = 0;
while (x > tmp) {
tmp = tmp * 10 + x % 10;
x /= 10;
}
return x == tmp || x == tmp / 10;
}
// 5 ms
public boolean isPalindrome1(int x) {
// public boolean isPalindrome(int x) {
if (x < 0)
return false;
int cur = 0;
int num = x;
while (num != 0) {
cur = cur * 10 + num % 10;
num /= 10;
}
return cur == x;
}
// practice
// 8ms
public boolean isPalindrome2(int x) {
// public boolean isPalindrome(int x) {
if (x < 0)
return false;
if (x < 10)
return true;
if (x % 10 == 0)
return false;
int tmp = 0;
while (tmp < x) {
tmp = tmp * 10 + x % 10;
x /= 10;
}
return x == tmp ? true : x == tmp / 10 ? true : false;
}
}