Skip to content

Commit 1b6bf53

Browse files
committed
update 0125 Solution for Java
1 parent 9d42a78 commit 1b6bf53

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

solution/0100-0199/0125.Valid Palindrome/Solution.java

+11-25
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
class Solution {
22
public boolean isPalindrome(String s) {
3-
if (s == null || s.length() == 0) return true;
4-
int start = 0 , end = s.length() - 1;
5-
while (start < end) {
6-
char c = ' ';
7-
while (start < end) {
8-
c = s.charAt(start);
9-
if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9') break;
10-
if (c >= 'A' && c <= 'Z') {
11-
c = (char) (c - 'A' + 'a');
12-
break;
13-
}
14-
start++;
3+
int i = 0;
4+
int j = s.length() - 1;
5+
while (i < j) {
6+
while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
7+
i++;
158
}
16-
char b = ' ';
17-
while (start < end) {
18-
b = s.charAt(end);
19-
if (b >= 'a' && b <= 'z' || b >= '0' && b <= '9') break;
20-
if (b >= 'A' && b <= 'Z') {
21-
b = (char) (b - 'A' + 'a');
22-
break;
23-
}
24-
end--;
9+
while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
10+
j--;
2511
}
26-
if (start < end) {
27-
if (c != b) return false;
28-
start++;
29-
end--;
12+
if (i < j && Character.toUpperCase(s.charAt(i)) != Character.toUpperCase(s.charAt(j))) {
13+
return false;
3014
}
15+
i++;
16+
j--;
3117
}
3218
return true;
3319
}

0 commit comments

Comments
 (0)