File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Integer.toBinaryString() 메서드 기억하기
3+ */
4+
5+
6+ import java .util .*;
7+
8+ class Solution {
9+ public int hammingWeight (int n ) {
10+ String s = Integer .toBinaryString (n );
11+ int count = 0 ;
12+
13+ for (int i =0 ; i <s .length (); i ++) {
14+ char c = s .charAt (i );
15+ if (c == 49 ) {
16+ count += 1 ;
17+ }
18+ }
19+
20+ return count ;
21+ }
22+ }
23+
Original file line number Diff line number Diff line change 1+ /*
2+ 1. 문제 이해
3+ 입력받은 글자에 대해 뒤집어도 동일한 글자인지 아닌지를 판단
4+
5+ 2. 예외 케이스
6+ 빈 문자열, 글자가 아닌 다른 내용일 경우 제거
7+
8+ 3. 알고리즘
9+ 단순 루프
10+
11+ 4. 구현
12+
13+ 글자를 입력받고 글자가 아닌 내용에 대해서는 필터링한다
14+ 전체 길이를 구하고 2포인터로 왼쪽, 오른쪽을 서로 줄여가면서
15+ 같거나 교차할때 종료
16+
17+ 다른 부분이 있으면 false 반환, 모두 통과하면 true 반환
18+
19+ 빈 값일경우 바로 true
20+
21+
22+ */
23+
24+ import java .util .*;
25+
26+ class Solution {
27+ public boolean isPalindrome (String s ) {
28+ String result = s .replaceAll ("[^a-zA-Z0-9]" , "" );
29+ result = result .toLowerCase ();
30+
31+ int len = result .length ();
32+ int left = 0 ;
33+ int right = len - 1 ;
34+
35+ while (left < right ) {
36+ char leftS = result .charAt (left );
37+ char rightS = result .charAt (right );
38+
39+ if (leftS != rightS ) {
40+ return false ;
41+ }
42+
43+ left += 1 ;
44+ right -= 1 ;
45+ }
46+
47+ return true ;
48+ }
49+ }
50+
You can’t perform that action at this time.
0 commit comments