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 11/*
2- Runtime: 1 ms(Beats: 100.00 %)
2+ Runtime: 2 ms(Beats: 99.10 %)
33Time Complexity: O(n)
44
5- Memory: 43.00 MB(Beats: 64.54 %)
5+ Memory: 42.75 MB(Beats: 85.31 %)
66Space Complexity: O(1)
77... 문제에서 주어진 String s는 space complexity 계산에서 제외하고, 제가 추가한 변수에 대해서만 계산하면 될까요?
8+
9+ ps. 처음 풀이에서는 alphanumeric 여부와 대소문자 관련 로직을 일일이 구현했다가, isLetterOrDigit(), toLowerCase()로 변경했습니다.
810*/
911
1012class 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 ;
You can’t perform that action at this time.
0 commit comments