Skip to content

Commit 8c8d7b1

Browse files
committed
refine
1 parent def822a commit 8c8d7b1

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

ValidPalindrome.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ public class ValidPalindrome {
1818
public boolean isPalindrome(String s) {
1919
s = s.toUpperCase();
2020
int i = 0, j = s.length() - 1;
21-
while (true) {
22-
while (i < s.length()
23-
&& !(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
24-
&& !(s.charAt(i) >= '0' && s.charAt(i) <= '9'))
21+
while (i < j) {
22+
if (!isAlphabet(s.charAt(i))) {
2523
i++;
26-
while (j >= 0 && !(s.charAt(j) >= 'A' && s.charAt(j) <= 'Z')
27-
&& !(s.charAt(j) >= '0' && s.charAt(j) <= '9'))
24+
} else if(!isAlphabet(s.charAt(j))) {
2825
j--;
29-
if (i >= j) {
30-
return true;
3126
} else if (s.charAt(i) != s.charAt(j)) {
3227
return false;
3328
} else {
3429
i++;
3530
j--;
3631
}
3732
}
33+
return true;
34+
}
35+
36+
private boolean isAlphabet(char c) {
37+
return (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
3838
}
3939
}

0 commit comments

Comments
 (0)