|
1 | 1 | class Solution {
|
2 | 2 | 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++; |
15 | 8 | }
|
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--; |
25 | 11 | }
|
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; |
30 | 14 | }
|
| 15 | + i++; |
| 16 | + j--; |
31 | 17 | }
|
32 | 18 | return true;
|
33 | 19 | }
|
|
0 commit comments