We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d8ddf63 commit c08fb7cCopy full SHA for c08fb7c
1 file changed
number-of-1-bits/hyeri0903.java
@@ -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