-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain18.java
More file actions
44 lines (42 loc) · 1.23 KB
/
Main18.java
File metadata and controls
44 lines (42 loc) · 1.23 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
package JZOfferTuJi;
public class Main18 {
public boolean isPalindrome(String s) {
if(s.length()==1){
return true;
}
String str = s.toLowerCase();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
if(Character.isLetterOrDigit(str.charAt(i))){
sb.append(str.charAt(i));
}
}
String ans = sb.toString();
for(int i = 0; i < ans.length()/2; i++){
if(ans.charAt(i)!=ans.charAt(ans.length()-1-i)){
return false;
}
}
return true;
}
}
class Main18_1{
public boolean isPalindrome(String s){
int left = 0;
int right = s.length() - 1;
while (left <= right){
if(!Character.isLetterOrDigit(s.charAt(left))){
left++;
}else if(!Character.isLetterOrDigit(s.charAt(right))){
right--;
}else {
char char1 = Character.toLowerCase(s.charAt(left++));
char char2 = Character.toLowerCase(s.charAt(right--));
if(char1 != char2){
return false;
}
}
}
return true;
}
}