Skip to content

Commit 2c123b1

Browse files
authored
feat: add rust solutions to lc problems: No.2894,3581 (#4502)
1 parent 589c8db commit 2c123b1

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ function differenceOfSums(n: number, m: number): number {
152152
}
153153
```
154154

155+
#### Rust
156+
157+
```rust
158+
impl Solution {
159+
pub fn difference_of_sums(n: i32, m: i32) -> i32 {
160+
let mut ans = 0;
161+
for i in 1..=n {
162+
if i % m != 0 {
163+
ans += i;
164+
} else {
165+
ans -= i;
166+
}
167+
}
168+
ans
169+
}
170+
}
171+
```
172+
155173
<!-- tabs:end -->
156174

157175
<!-- solution:end -->

solution/2800-2899/2894.Divisible and Non-divisible Sums Difference/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ function differenceOfSums(n: number, m: number): number {
150150
}
151151
```
152152

153+
#### Rust
154+
155+
```rust
156+
impl Solution {
157+
pub fn difference_of_sums(n: i32, m: i32) -> i32 {
158+
let mut ans = 0;
159+
for i in 1..=n {
160+
if i % m != 0 {
161+
ans += i;
162+
} else {
163+
ans -= i;
164+
}
165+
}
166+
ans
167+
}
168+
}
169+
```
170+
153171
<!-- tabs:end -->
154172

155173
<!-- solution:end -->

solution/3500-3599/3581.Count Odd Letters from Number/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,47 @@ function bitCount(i: number): number {
233233
}
234234
```
235235

236+
#### Rust
237+
238+
```rust
239+
impl Solution {
240+
pub fn count_odd_letters(mut n: i32) -> i32 {
241+
use std::collections::HashMap;
242+
243+
let d: HashMap<i32, &str> = [
244+
(0, "zero"),
245+
(1, "one"),
246+
(2, "two"),
247+
(3, "three"),
248+
(4, "four"),
249+
(5, "five"),
250+
(6, "six"),
251+
(7, "seven"),
252+
(8, "eight"),
253+
(9, "nine"),
254+
]
255+
.iter()
256+
.cloned()
257+
.collect();
258+
259+
let mut mask: u32 = 0;
260+
261+
while n > 0 {
262+
let x = n % 10;
263+
n /= 10;
264+
if let Some(word) = d.get(&x) {
265+
for c in word.chars() {
266+
let bit = 1 << (c as u8 - b'a');
267+
mask ^= bit as u32;
268+
}
269+
}
270+
}
271+
272+
mask.count_ones() as i32
273+
}
274+
}
275+
```
276+
236277
<!-- tabs:end -->
237278

238279
<!-- solution:end -->

solution/3500-3599/3581.Count Odd Letters from Number/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,47 @@ function bitCount(i: number): number {
233233
}
234234
```
235235

236+
#### Rust
237+
238+
```rust
239+
impl Solution {
240+
pub fn count_odd_letters(mut n: i32) -> i32 {
241+
use std::collections::HashMap;
242+
243+
let d: HashMap<i32, &str> = [
244+
(0, "zero"),
245+
(1, "one"),
246+
(2, "two"),
247+
(3, "three"),
248+
(4, "four"),
249+
(5, "five"),
250+
(6, "six"),
251+
(7, "seven"),
252+
(8, "eight"),
253+
(9, "nine"),
254+
]
255+
.iter()
256+
.cloned()
257+
.collect();
258+
259+
let mut mask: u32 = 0;
260+
261+
while n > 0 {
262+
let x = n % 10;
263+
n /= 10;
264+
if let Some(word) = d.get(&x) {
265+
for c in word.chars() {
266+
let bit = 1 << (c as u8 - b'a');
267+
mask ^= bit as u32;
268+
}
269+
}
270+
}
271+
272+
mask.count_ones() as i32
273+
}
274+
}
275+
```
276+
236277
<!-- tabs:end -->
237278

238279
<!-- solution:end -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
impl Solution {
2+
pub fn count_odd_letters(mut n: i32) -> i32 {
3+
use std::collections::HashMap;
4+
5+
let d: HashMap<i32, &str> = [
6+
(0, "zero"),
7+
(1, "one"),
8+
(2, "two"),
9+
(3, "three"),
10+
(4, "four"),
11+
(5, "five"),
12+
(6, "six"),
13+
(7, "seven"),
14+
(8, "eight"),
15+
(9, "nine"),
16+
]
17+
.iter()
18+
.cloned()
19+
.collect();
20+
21+
let mut mask: u32 = 0;
22+
23+
while n > 0 {
24+
let x = n % 10;
25+
n /= 10;
26+
if let Some(word) = d.get(&x) {
27+
for c in word.chars() {
28+
let bit = 1 << (c as u8 - b'a');
29+
mask ^= bit as u32;
30+
}
31+
}
32+
}
33+
34+
mask.count_ones() as i32
35+
}
36+
}

0 commit comments

Comments
 (0)