Skip to content

Commit 7885773

Browse files
committed
feat: update solutions to leetcode problem: No.0191
1 parent ed0233e commit 7885773

File tree

6 files changed

+98
-17
lines changed

6 files changed

+98
-17
lines changed

lcof/面试题15. 二进制中1的个数/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
`n & (n - 1)` 会消除 n 中最后一位中的 1。
3434

35+
[LeetCode 191. 位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md)
36+
3537
<!-- tabs:start -->
3638

3739
### **Python3**

solution/0100-0199/0191.Number of 1 Bits/README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,59 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
`n & (n - 1)` 会消除 n 中最后一位中的 1。
51+
52+
[剑指 Offer 15. 二进制中 1 的个数](/lcof/面试题15.%20二进制中1的个数/README.md)
53+
5054
<!-- tabs:start -->
5155

5256
### **Python3**
5357

5458
<!-- 这里可写当前语言的特殊实现逻辑 -->
5559

5660
```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
5868
```
5969

6070
### **Java**
6171

6272
<!-- 这里可写当前语言的特殊实现逻辑 -->
6373

6474
```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+
```
6587

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+
};
66103
```
67104

68105
### **...**

solution/0100-0199/0191.Number of 1 Bits/README_EN.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,46 @@
6464
### **Python3**
6565

6666
```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
6874
```
6975

7076
### **Java**
7177

7278
```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+
```
7391

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+
};
74107
```
75108

76109
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
public class Solution {
2+
// you need to treat n as an unsigned value
23
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;
410
}
5-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
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;
37
while (n) {
4-
result += n & 1;
5-
n = n >>> 1;
8+
n &= n - 1;
9+
++res;
610
}
7-
return result;
11+
return res;
812
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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

0 commit comments

Comments
 (0)