Skip to content

Commit 6a12988

Browse files
number of 1 bits solution
1 parent a8d6265 commit 6a12988

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

number-of-1-bits/jaejeong1.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class SolutionJaejeong1 {
2+
public int hammingWeight(int n) {
3+
// 주어진 양의 정수를 이진수로 변환하고, 1로 설정된 자릿수의 개수를 반환
4+
// 시간복잡도: O(1), 공간복잡도: O(1)
5+
int num = 0;
6+
for (int i=0; i<32; i++) {
7+
if((n & 1) == 1) {
8+
num++;
9+
}
10+
n = n >> 1;
11+
}
12+
return num;
13+
}
14+
}

0 commit comments

Comments
 (0)