Skip to content

Commit 277e417

Browse files
committed
#232 number-of-1-bits solution
1 parent 2488680 commit 277e417

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

number-of-1-bits/sungjinwi.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
풀이 :
3+
쉬프트 연산으로 n을 감소 시키면서 n 과 1의 & 연산이 true인 갯수를 세서 리턴
4+
5+
TC : O(1)
6+
n이 커도 최대 32번의 반복문
7+
8+
SC : O(1)
9+
*/
10+
11+
class Solution {
12+
public:
13+
int hammingWeight(int n) {
14+
int cnt = 0;
15+
while (n > 0)
16+
{
17+
if (n & 1)
18+
cnt++;
19+
n = n >> 1;
20+
}
21+
return cnt;
22+
}
23+
};

0 commit comments

Comments
 (0)