Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 37aa93a

Browse files
feat: add rust solution to lc problem: No.0191 (doocs#784)
No.0191.Number of 1 Bits
1 parent ee18f27 commit 37aa93a

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,39 @@
6565

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

68-
`n & (n - 1)` 会消除 n 中最后一位中的 1。
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+
```
78+
79+
利用 `n & (n - 1)` 消除 `n` 中最后一位 1 这一特点,优化过程:
80+
81+
```txt
82+
HAMMING-WEIGHT(n)
83+
r = 0
84+
while n != 0
85+
n &= n - 1
86+
r += 1
87+
r
88+
```
89+
90+
以 5 为例,演示推演过程:
91+
92+
```txt
93+
[0, 1, 0, 1] // 5
94+
[0, 1, 0, 0] // 5 - 1 = 4
95+
[0, 1, 0, 0] // 5 & 4 = 4
96+
97+
[0, 1, 0, 0] // 4
98+
[0, 0, 1, 1] // 4 - 1 = 3
99+
[0, 0, 0, 0] // 4 & 3 = 0
100+
```
69101

70102
[剑指 Offer 15. 二进制中 1 的个数](/lcof/面试题15.%20二进制中1的个数/README.md)
71103

@@ -150,6 +182,42 @@ var hammingWeight = function (n) {
150182
};
151183
```
152184

185+
### **Rust**
186+
187+
```rust
188+
impl Solution {
189+
pub fn hammingWeight(n: u32) -> i32 {
190+
n.count_ones() as i32
191+
}
192+
}
193+
```
194+
195+
```rust
196+
impl Solution {
197+
pub fn hammingWeight(mut n: u32) -> i32 {
198+
let mut res = 0;
199+
while n != 0 {
200+
res += n & 1;
201+
n >>= 1;
202+
}
203+
res as i32
204+
}
205+
}
206+
```
207+
208+
```rust
209+
impl Solution {
210+
pub fn hammingWeight(mut n: u32) -> i32 {
211+
let mut res = 0;
212+
while n != 0 {
213+
n &= (n - 1);
214+
res += 1;
215+
}
216+
res
217+
}
218+
}
219+
```
220+
153221
### **...**
154222

155223
```

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,42 @@ var hammingWeight = function (n) {
127127
};
128128
```
129129

130+
### **Rust**
131+
132+
```rust
133+
impl Solution {
134+
pub fn hammingWeight(n: u32) -> i32 {
135+
n.count_ones() as i32
136+
}
137+
}
138+
```
139+
140+
```rust
141+
impl Solution {
142+
pub fn hammingWeight(mut n: u32) -> i32 {
143+
let mut res = 0;
144+
while n != 0 {
145+
res += n & 1;
146+
n >>= 1;
147+
}
148+
res as i32
149+
}
150+
}
151+
```
152+
153+
```rust
154+
impl Solution {
155+
pub fn hammingWeight(mut n: u32) -> i32 {
156+
let mut res = 0;
157+
while n != 0 {
158+
n &= (n - 1);
159+
res += 1;
160+
}
161+
res
162+
}
163+
}
164+
```
165+
130166
### **...**
131167

132168
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn hammingWeight(n: u32) -> i32 {
3+
n.count_ones() as i32
4+
}
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /