Skip to content

Commit 19d1243

Browse files
committed
Longest Consecutive Sequence - Character 메소드 사용
1 parent a6fd4e2 commit 19d1243

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

valid-palindrome/forest000014.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
/*
2-
Runtime: 1 ms(Beats: 100.00 %)
2+
Runtime: 2 ms(Beats: 99.10 %)
33
Time Complexity: O(n)
44
5-
Memory: 43.00 MB(Beats: 64.54 %)
5+
Memory: 42.75 MB(Beats: 85.31 %)
66
Space Complexity: O(1)
77
... 문제에서 주어진 String s는 space complexity 계산에서 제외하고, 제가 추가한 변수에 대해서만 계산하면 될까요?
8+
9+
ps. 처음 풀이에서는 alphanumeric 여부와 대소문자 관련 로직을 일일이 구현했다가, isLetterOrDigit(), toLowerCase()로 변경했습니다.
810
*/
911

1012
class Solution {
1113
public boolean isPalindrome(String s) {
1214
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
1315
char a = s.charAt(i);
14-
while (a < '0' || (a > '9' && a < 'A') || (a > 'Z' && a < 'a') || a > 'z') {
16+
while (!Character.isLetterOrDigit(a)) {
1517
a = s.charAt(++i);
1618
if (i >= j) {
1719
return true;
1820
}
1921
}
20-
if (a <= 'Z') {
21-
a += ('a' - 'A');
22-
}
22+
a = Character.toLowerCase(a);
2323

2424
char b = s.charAt(j);
25-
while (b < '0' || (b > '9' && b < 'A') || (b > 'Z' && b < 'a') || b > 'z') {
25+
while (!Character.isLetterOrDigit(b)) {
2626
b = s.charAt(--j);
2727
if (i >= j) {
2828
return true;
2929
}
3030
}
31-
if (b <= 'Z') {
32-
b += ('a' - 'A');
33-
}
31+
b = Character.toLowerCase(b);
3432

3533
if (a != b) {
3634
return false;

0 commit comments

Comments
 (0)