Skip to content

Commit c08fb7c

Browse files
committed
number-of-1-bits solution
1 parent d8ddf63 commit c08fb7c

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

number-of-1-bits/hyeri0903.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
/**
4+
1.문제: 이진수 변환 후 1의 개수 반환
5+
2. 2로 나누면서 나머지가 1일 경우 count+=1
6+
*/
7+
int count = 0;
8+
// while(n > 0) {
9+
// if (n % 2 != 0) {
10+
// count += 1;
11+
// }
12+
// n = n / 2;
13+
// }
14+
15+
while(n > 0) {
16+
if ((n & 1) == 1) {
17+
count += 1;
18+
}
19+
n >>= 1;
20+
}
21+
return count;
22+
}
23+
}

0 commit comments

Comments
 (0)