File tree Expand file tree Collapse file tree 1 file changed +73
-0
lines changed Expand file tree Collapse file tree 1 file changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ [문제풀이]
3+ - 대문자 -> 소문자 변환
4+ - 소문자가 아닌 문자면 넘어가고, 소문자만 체크
5+ - 숫자는 넘어갈 수 없다.
6+ - 소문자 input, reverse input 비교
7+
8+ - 풀이1
9+ time: O(N), space: O(1)
10+ class Solution {
11+ public boolean isPalindrome(String s) {
12+ if (s.trim().length() == 0) {
13+ return true;
14+ }
15+
16+ s = s.toLowerCase();
17+ int left = 0;
18+ int right = s.length() - 1;
19+ while (left < right) {
20+ while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
21+ left++;
22+ }
23+ while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
24+ right--;
25+ }
26+
27+ if (s.charAt(left) != s.charAt(right)) {
28+ return false;
29+ }
30+ left++;
31+ right--;
32+ }
33+ return true;
34+ }
35+ }
36+
37+ - 풀이2
38+ time: O(N), space: O(1)
39+
40+ [회고]
41+ 중간에 공백을 replace하면 안좋을 것 같은데..
42+ -> 알파벳인지를 비교해서 인덱스를 바꾸자!
43+
44+ for문만 고집하지 말고, while문도 사용해보자!
45+ (처음에 for문으로 풀다가 스파게티가 되어버렸ㄷ..)
46+
47+ !!! String.toLowerCase() 로 소문자로 변환하는 것이 좋은 방법일까?? !!!
48+ */
49+
50+ class Solution {
51+ public boolean isPalindrome (String s ) {
52+ s = s .toLowerCase ();
53+ int left = 0 ;
54+ int right = s .length () - 1 ;
55+ while (left < right ) {
56+ if (!Character .isLetterOrDigit (s .charAt (left ))) {
57+ left ++;
58+ continue ;
59+ }
60+ if (!Character .isLetterOrDigit (s .charAt (right ))) {
61+ right --;
62+ continue ;
63+ }
64+
65+ if (s .charAt (left ) != s .charAt (right )) {
66+ return false ;
67+ }
68+ left ++;
69+ right --;
70+ }
71+ return true ;
72+ }
73+ }
You can’t perform that action at this time.
0 commit comments