forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125_isPalindrome.java
More file actions
27 lines (25 loc) · 797 Bytes
/
Copy path125_isPalindrome.java
File metadata and controls
27 lines (25 loc) · 797 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
public class Solution {
public boolean isPalindrome(String s) {
if(s == null || s.length() == 0) return true;
s = s.trim().toUpperCase();
if(s.length()==0) return true;
int left = 0;
int right = s.length()-1;
while(left<right){
while(left <= s.length()-1 && !isNumberLetter(s.charAt(left))){
left++;
}
while(right >= 0 && !isNumberLetter(s.charAt(right))){
right--;
}
if(left > right) break;
if(s.charAt(left)!=s.charAt(right)) return false;
left++;
right--;
}
return true;
}
public boolean isNumberLetter(char c){
return (c >= 'A' && c<='Z') ||(c>='0'&& c<='9');
}
}