Skip to content

Commit d4a3f56

Browse files
committed
3주차 문제 풀이 1개 추가
- Number of 1 Bits
1 parent 6fa0ef0 commit d4a3f56

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

number-of-1-bits/hwi-middle.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int hammingWeight(int n) {
4+
// 아주 간편한 풀이가 있지만...
5+
// return __builtin_popcount(n);
6+
7+
// 직접 해보기
8+
int cnt = 0;
9+
for (unsigned int i = 1 << 31; i > 0; i /= 2)
10+
{
11+
if ((n & i) != 0)
12+
{
13+
cnt++;
14+
}
15+
}
16+
17+
return cnt;
18+
}
19+
};

0 commit comments

Comments
 (0)