File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ [λ¬Έμ νμ΄]
3
+ - μ£Όμ΄μ§ μμ μ μλ₯Ό 2μ§μλ‘ λ³ννμ¬, 1μ κ°μ ꡬνκΈ°
4
+
5
+ - νμ΄ 1
6
+ time: O(log N), space: O(1)
7
+ class Solution {
8
+ public int hammingWeight(int n) {
9
+ int count = 0;
10
+ while (n > 0) {
11
+ if (n % 2 != 0) {
12
+ count++;
13
+ }
14
+ n /= 2;
15
+ }
16
+ return count;
17
+ }
18
+ }
19
+ - νμ΄ 2
20
+ time: O(log N), space: O(1)
21
+
22
+ [νκ³ ]
23
+ `n >> 1`λΉνΈ μ°μ°μλ `n / 2` μ κ°λ€.
24
+ `n >> 1`λ‘ νλ©΄ λΉκ΅λ λΉνΈλ‘!
25
+
26
+ μ΄μ§ ννμμ 1μ κ°μλ₯Ό μΈμ΄μ£Όλ Integer.bitCount() λ©μλλ μμλ€.
27
+ */
28
+ class Solution {
29
+ public int hammingWeight (int n ) {
30
+ int count = 0 ;
31
+ while (n > 0 ) {
32
+ if ((n & 1 ) == 1 ) {
33
+ count ++;
34
+ }
35
+ n >>= 1 ;
36
+ }
37
+ return count ;
38
+ }
39
+ }
You canβt perform that action at this time.
0 commit comments