File tree 6 files changed +98
-17
lines changed
solution/0100-0199/0191.Number of 1 Bits
6 files changed +98
-17
lines changed Original file line number Diff line number Diff line change 32
32
33
33
` n & (n - 1) ` 会消除 n 中最后一位中的 1。
34
34
35
+ 同 [ LeetCode 191. 位 1 的个数] ( /solution/0100-0199/0191.Number%20of%201%20Bits/README.md )
36
+
35
37
<!-- tabs:start -->
36
38
37
39
### ** Python3**
Original file line number Diff line number Diff line change 47
47
48
48
<!-- 这里可写通用的实现逻辑 -->
49
49
50
+ ` n & (n - 1) ` 会消除 n 中最后一位中的 1。
51
+
52
+ 同 [ 剑指 Offer 15. 二进制中 1 的个数] ( /lcof/面试题15.%20二进制中1的个数/README.md )
53
+
50
54
<!-- tabs:start -->
51
55
52
56
### ** Python3**
53
57
54
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
59
56
60
``` python
57
-
61
+ class Solution :
62
+ def hammingWeight (self , n : int ) -> int :
63
+ res = 0
64
+ while n:
65
+ n &= (n - 1 )
66
+ res += 1
67
+ return res
58
68
```
59
69
60
70
### ** Java**
61
71
62
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
73
64
74
``` java
75
+ public class Solution {
76
+ // you need to treat n as an unsigned value
77
+ public int hammingWeight (int n ) {
78
+ int res = 0 ;
79
+ while (n != 0 ) {
80
+ n &= (n - 1 );
81
+ ++ res;
82
+ }
83
+ return res;
84
+ }
85
+ }
86
+ ```
65
87
88
+ ### ** JavaScript**
89
+
90
+ ``` js
91
+ /**
92
+ * @param {number} n - a positive integer
93
+ * @return {number}
94
+ */
95
+ var hammingWeight = function (n ) {
96
+ let res = 0 ;
97
+ while (n) {
98
+ n &= n - 1 ;
99
+ ++ res;
100
+ }
101
+ return res;
102
+ };
66
103
```
67
104
68
105
### ** ...**
Original file line number Diff line number Diff line change 64
64
### ** Python3**
65
65
66
66
``` python
67
-
67
+ class Solution :
68
+ def hammingWeight (self , n : int ) -> int :
69
+ res = 0
70
+ while n:
71
+ n &= (n - 1 )
72
+ res += 1
73
+ return res
68
74
```
69
75
70
76
### ** Java**
71
77
72
78
``` java
79
+ public class Solution {
80
+ // you need to treat n as an unsigned value
81
+ public int hammingWeight (int n ) {
82
+ int res = 0 ;
83
+ while (n != 0 ) {
84
+ n &= (n - 1 );
85
+ ++ res;
86
+ }
87
+ return res;
88
+ }
89
+ }
90
+ ```
73
91
92
+ ### ** JavaScript**
93
+
94
+ ``` js
95
+ /**
96
+ * @param {number} n - a positive integer
97
+ * @return {number}
98
+ */
99
+ var hammingWeight = function (n ) {
100
+ let res = 0 ;
101
+ while (n) {
102
+ n &= n - 1 ;
103
+ ++ res;
104
+ }
105
+ return res;
106
+ };
74
107
```
75
108
76
109
### ** ...**
Original file line number Diff line number Diff line change 1
1
public class Solution {
2
+ // you need to treat n as an unsigned value
2
3
public int hammingWeight (int n ) {
3
- return Integer .bitCount (n );
4
+ int res = 0 ;
5
+ while (n != 0 ) {
6
+ n &= (n - 1 );
7
+ ++res ;
8
+ }
9
+ return res ;
4
10
}
5
- }
11
+ }
Original file line number Diff line number Diff line change 1
- const hammingWeight = function ( n ) {
2
- let result = 0 ;
1
+ /**
2
+ * @param {number } n - a positive integer
3
+ * @return {number }
4
+ */
5
+ var hammingWeight = function ( n ) {
6
+ let res = 0 ;
3
7
while ( n ) {
4
- result + = n & 1 ;
5
- n = n >>> 1 ;
8
+ n & = n - 1 ;
9
+ ++ res ;
6
10
}
7
- return result ;
11
+ return res ;
8
12
} ;
Original file line number Diff line number Diff line change 1
- class Solution (object ):
2
- def hammingWeight (self , n ):
3
- """
4
- :type n: int
5
- :rtype: int
6
- """
7
-
8
- return bin (n ).count ("1" )
1
+ class Solution :
2
+ def hammingWeight (self , n : int ) -> int :
3
+ res = 0
4
+ while n :
5
+ n &= (n - 1 )
6
+ res += 1
7
+ return res
You can’t perform that action at this time.
0 commit comments