-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeNumber.java
More file actions
36 lines (34 loc) · 962 Bytes
/
PalindromeNumber.java
File metadata and controls
36 lines (34 loc) · 962 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
package com.leetcode;
/**
* Created by jamylu on 2017/12/31.
* leetcode009.回文数判断,通过比较每一位
*/
public class PalindromeNumber {
public static void main(String args[]) {
int x = -2147447412;
System.out.println(isPalindrome(x));
}
public static boolean isPalindrome(int x) {
//负数直接返回false
if (x < 0) {
return false;
}
x = Math.abs(x);
int tmp = 1;
// tmp 和 x的位数保持一致
while (x / tmp >= 10) {
tmp *= 10;
}
while (x != 0) {
// 最高位和最低位比较
// System.out.println(x / tmp + " " + x % 10);
if (x / tmp != x % 10) {
return false;
}
// 去掉最高位和最低位,tmp 缩小100倍,和x位数保持一致
x = (x % tmp) / 10;
tmp /= 100;
}
return true;
}
}