LeetCode刷题之Palindrome_Number_0009

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
public static void main(String[] args) {
System.out.println(isPalindrome_Double_Direction(-121));
}

//根据0007 Reverse_integer的经验 讲整数逆序与原整数比较大小
public static boolean isPalindrome(int x) {
if( x < 0) return false;
if( x < 10) return true;

if (x % 10 == 0) return false;

int n = 0,init = x;
while(x != 0){
n = n * 10 + x % 10;
x = x / 10;
}

return init == n;
}

//回文序列检查
public static boolean isPalindrome_Double_Direction(int x) {
if( x < 0){
return false;
}
char[] ArrX = String.valueOf(x).toCharArray();
int middleIndex = ArrX.length / 2 - 1;
for(int i = 0; i <= middleIndex;i++){
if(ArrX[i] != ArrX[ArrX.length-1-i])
return false;
}
return true;
}
苏小南 wechat
扫一扫,点个关注哦!
生活不易,记得打个赏
Powered By Valine
v1.5.2