Skip to content

Commit 399fa00

Browse files
committed
feat: update solutions to lc problem: No.0191
No.0191.Number of 1 Bits
1 parent ae9321c commit 399fa00

File tree

5 files changed

+126
-23
lines changed

5 files changed

+126
-23
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
- [区域和检索 - 数组不可变](/solution/0300-0399/0303.Range%20Sum%20Query%20-%20Immutable/README.md) - 前缀和
4545
- [二维区域和检索 - 矩阵不可变](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README.md) - 二维前缀和
4646
- [区间加法](/solution/0300-0399/0370.Range%20Addition/README.md) - 前缀和、差分
47-
- [ 用邮票贴满网格图](/solution/2100-2199/2132.Stamping%20the%20Grid/README.md) - 二维前缀和、二维差分
47+
- [用邮票贴满网格图](/solution/2100-2199/2132.Stamping%20the%20Grid/README.md) - 二维前缀和、二维差分
48+
- [无重复字符的最长子串](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README.md) - 双指针、哈希表
49+
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - 位运算、lowbit
50+
4851
<!-- 排序算法、待补充 -->
4952

5053
### 2. 搜索

README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
4444
- [Range Sum Query 2D - Immutable](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README_EN.md) - Prefix sum
4545
- [Range Addition](/solution/0300-0399/0370.Range%20Addition/README_EN.md) - Prefix sum, Difference array
4646
- [ Stamping the Grid](/solution/2100-2199/2132.Stamping%20the%20Grid/README_EN.md) - Prefix sum, Difference array
47+
- [Longest Substring Without Repeating Characters](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README_EN.md) - Two pointers, Hash table
48+
- [Number of 1 Bits](/solution/0100-0199/0191.Number%20of%201%20Bits/README_EN.md) - Bit manipulation, Lowbit
4749

4850
### 2. Search
4951

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@
4848

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

51-
“滑动窗口 + 哈希表”。
51+
**方法一:双指针 + 哈希表**
5252

5353
定义一个哈希表记录当前窗口内出现的字符,i、j 分别表示不重复子串的开始位置和结束位置,ans 表示无重复字符子串的最大长度。
5454

55-
遍历 s 每个字符 c,若 `[i, j - 1]` 窗口内存在 `c`,则 i 循环向右移动,更新哈希表,直至 `[i, j - 1]` 窗口不存在 `c`,循环结束。将 `c` 加入哈希表中,此时 `[i, j]` 窗口内不含重复元素,更新 ans 的最大值:`res = max(ans, j - i + 1)`
55+
遍历 s 每个字符 c,若 `[i, j - 1]` 窗口内存在 `c`,则 i 循环向右移动,更新哈希表,直至 `[i, j - 1]` 窗口不存在 `c`,循环结束。将 `c` 加入哈希表中,此时 `[i, j]` 窗口内不含重复元素,更新 ans 的最大值:`ans = max(ans, j - i + 1)`
5656

5757
最后返回 ans 即可。
5858

59+
时间复杂度 O(n),其中 n 表示字符串 s 的长度。
60+
5961
<!-- tabs:start -->
6062

6163
### **Python3**

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

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,7 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68-
朴素解法:一位一位数。
69-
70-
```txt
71-
HAMMING-WEIGHT(n)
72-
r = 0
73-
while n != 0
74-
r += n & 1
75-
n >>= 1
76-
r
77-
```
68+
**方法一:位运算**
7869

7970
利用 `n & (n - 1)` 消除 `n` 中最后一位 1 这一特点,优化过程:
8071

@@ -99,6 +90,10 @@ HAMMING-WEIGHT(n)
9990
[0, 0, 0, 0] // 4 & 3 = 0
10091
```
10192

93+
**方法二:lowbit**
94+
95+
`x -= (x & -x)` 可以消除二进制形式的最后一位 1。
96+
10297
[剑指 Offer 15. 二进制中 1 的个数](/lcof/面试题15.%20二进制中1的个数/README.md)
10398

10499
<!-- tabs:start -->
@@ -117,6 +112,16 @@ class Solution:
117112
return ans
118113
```
119114

115+
```python
116+
class Solution:
117+
def hammingWeight(self, n: int) -> int:
118+
ans = 0
119+
while n:
120+
n -= (n & -n)
121+
ans += 1
122+
return ans
123+
```
124+
120125
### **Java**
121126

122127
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -135,16 +140,17 @@ public class Solution {
135140
}
136141
```
137142

138-
### **Go**
139-
140-
```go
141-
func hammingWeight(num uint32) int {
142-
ans := 0
143-
for num != 0 {
144-
num &= num - 1
145-
ans++
146-
}
147-
return ans
143+
```java
144+
public class Solution {
145+
// you need to treat n as an unsigned value
146+
public int hammingWeight(int n) {
147+
int ans = 0;
148+
while (n != 0) {
149+
n -= (n & -n);
150+
++ans;
151+
}
152+
return ans;
153+
}
148154
}
149155
```
150156

@@ -165,6 +171,45 @@ public:
165171
};
166172
```
167173
174+
```cpp
175+
class Solution {
176+
public:
177+
int hammingWeight(uint32_t n) {
178+
int ans = 0;
179+
while (n)
180+
{
181+
n -= (n & -n);
182+
++ans;
183+
}
184+
return ans;
185+
}
186+
};
187+
```
188+
189+
### **Go**
190+
191+
```go
192+
func hammingWeight(num uint32) int {
193+
ans := 0
194+
for num != 0 {
195+
num &= num - 1
196+
ans++
197+
}
198+
return ans
199+
}
200+
```
201+
202+
```go
203+
func hammingWeight(num uint32) int {
204+
ans := 0
205+
for num != 0 {
206+
num -= (num & -num)
207+
ans++
208+
}
209+
return ans
210+
}
211+
```
212+
168213
### **JavaScript**
169214

170215
```js

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ class Solution:
6464
return ans
6565
```
6666

67+
```python
68+
class Solution:
69+
def hammingWeight(self, n: int) -> int:
70+
ans = 0
71+
while n:
72+
n -= (n & -n)
73+
ans += 1
74+
return ans
75+
```
76+
6777
### **Java**
6878

6979
```java
@@ -80,6 +90,20 @@ public class Solution {
8090
}
8191
```
8292

93+
```java
94+
public class Solution {
95+
// you need to treat n as an unsigned value
96+
public int hammingWeight(int n) {
97+
int ans = 0;
98+
while (n != 0) {
99+
n -= (n & -n);
100+
++ans;
101+
}
102+
return ans;
103+
}
104+
}
105+
```
106+
83107
### **C++**
84108

85109
```cpp
@@ -97,6 +121,21 @@ public:
97121
};
98122
```
99123
124+
```cpp
125+
class Solution {
126+
public:
127+
int hammingWeight(uint32_t n) {
128+
int ans = 0;
129+
while (n)
130+
{
131+
n -= (n & -n);
132+
++ans;
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
100139
### **Go**
101140

102141
```go
@@ -110,6 +149,17 @@ func hammingWeight(num uint32) int {
110149
}
111150
```
112151

152+
```go
153+
func hammingWeight(num uint32) int {
154+
ans := 0
155+
for num != 0 {
156+
num -= (num & -num)
157+
ans++
158+
}
159+
return ans
160+
}
161+
```
162+
113163
### **JavaScript**
114164

115165
```js
@@ -127,6 +177,7 @@ var hammingWeight = function (n) {
127177
};
128178
```
129179

180+
130181
### **Rust**
131182

132183
```rust

0 commit comments

Comments
 (0)