File tree Expand file tree Collapse file tree 1 file changed +8
-10
lines changed Expand file tree Collapse file tree 1 file changed +8
-10
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- Runtime: 1 ms(Beats: 100.00 %)
2
+ Runtime: 2 ms(Beats: 99.10 %)
3
3
Time Complexity: O(n)
4
4
5
- Memory: 43.00 MB(Beats: 64.54 %)
5
+ Memory: 42.75 MB(Beats: 85.31 %)
6
6
Space Complexity: O(1)
7
7
... 문제에서 주어진 String s는 space complexity 계산에서 제외하고, 제가 추가한 변수에 대해서만 계산하면 될까요?
8
+
9
+ ps. 처음 풀이에서는 alphanumeric 여부와 대소문자 관련 로직을 일일이 구현했다가, isLetterOrDigit(), toLowerCase()로 변경했습니다.
8
10
*/
9
11
10
12
class Solution {
11
13
public boolean isPalindrome (String s ) {
12
14
for (int i = 0 , j = s .length () - 1 ; i < j ; i ++, j --) {
13
15
char a = s .charAt (i );
14
- while (a < '0' || ( a > '9' && a < 'A' ) || ( a > 'Z' && a < 'a' ) || a > 'z' ) {
16
+ while (! Character . isLetterOrDigit ( a ) ) {
15
17
a = s .charAt (++i );
16
18
if (i >= j ) {
17
19
return true ;
18
20
}
19
21
}
20
- if (a <= 'Z' ) {
21
- a += ('a' - 'A' );
22
- }
22
+ a = Character .toLowerCase (a );
23
23
24
24
char b = s .charAt (j );
25
- while (b < '0' || ( b > '9' && b < 'A' ) || ( b > 'Z' && b < 'a' ) || b > 'z' ) {
25
+ while (! Character . isLetterOrDigit ( b ) ) {
26
26
b = s .charAt (--j );
27
27
if (i >= j ) {
28
28
return true ;
29
29
}
30
30
}
31
- if (b <= 'Z' ) {
32
- b += ('a' - 'A' );
33
- }
31
+ b = Character .toLowerCase (b );
34
32
35
33
if (a != b ) {
36
34
return false ;
You can’t perform that action at this time.
0 commit comments