Skip to content

Commit 0a193f5

Browse files
committed
feat: add rust solution to lc problem: No.3583
1 parent cee45f2 commit 0a193f5

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

solution/3500-3599/3583.Count Special Triplets/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,43 @@ function specialTriplets(nums: number[]): number {
239239
}
240240
```
241241

242+
#### Rust
243+
244+
```rust
245+
use std::collections::HashMap;
246+
247+
impl Solution {
248+
pub fn special_triplets(nums: Vec<i32>) -> i32 {
249+
let mut left: HashMap<i32, i64> = HashMap::new();
250+
let mut right: HashMap<i32, i64> = HashMap::new();
251+
252+
for &x in &nums {
253+
*right.entry(x).or_insert(0) += 1;
254+
}
255+
256+
let modulo: i64 = 1_000_000_007;
257+
let mut ans: i64 = 0;
258+
259+
for &x in &nums {
260+
if let Some(v) = right.get_mut(&x) {
261+
*v -= 1;
262+
}
263+
264+
let t = x * 2;
265+
266+
let l = *left.get(&t).unwrap_or(&0);
267+
let r = *right.get(&t).unwrap_or(&0);
268+
269+
ans = (ans + (l * r) % modulo) % modulo;
270+
271+
*left.entry(x).or_insert(0) += 1;
272+
}
273+
274+
ans as i32
275+
}
276+
}
277+
```
278+
242279
<!-- tabs:end -->
243280

244281
<!-- solution:end -->

solution/3500-3599/3583.Count Special Triplets/README_EN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,43 @@ function specialTriplets(nums: number[]): number {
237237
}
238238
```
239239

240+
#### Rust
241+
242+
```rust
243+
use std::collections::HashMap;
244+
245+
impl Solution {
246+
pub fn special_triplets(nums: Vec<i32>) -> i32 {
247+
let mut left: HashMap<i32, i64> = HashMap::new();
248+
let mut right: HashMap<i32, i64> = HashMap::new();
249+
250+
for &x in &nums {
251+
*right.entry(x).or_insert(0) += 1;
252+
}
253+
254+
let modulo: i64 = 1_000_000_007;
255+
let mut ans: i64 = 0;
256+
257+
for &x in &nums {
258+
if let Some(v) = right.get_mut(&x) {
259+
*v -= 1;
260+
}
261+
262+
let t = x * 2;
263+
264+
let l = *left.get(&t).unwrap_or(&0);
265+
let r = *right.get(&t).unwrap_or(&0);
266+
267+
ans = (ans + (l * r) % modulo) % modulo;
268+
269+
*left.entry(x).or_insert(0) += 1;
270+
}
271+
272+
ans as i32
273+
}
274+
}
275+
```
276+
240277
<!-- tabs:end -->
241278

242279
<!-- solution:end -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn special_triplets(nums: Vec<i32>) -> i32 {
5+
let mut left: HashMap<i32, i64> = HashMap::new();
6+
let mut right: HashMap<i32, i64> = HashMap::new();
7+
8+
for &x in &nums {
9+
*right.entry(x).or_insert(0) += 1;
10+
}
11+
12+
let modulo: i64 = 1_000_000_007;
13+
let mut ans: i64 = 0;
14+
15+
for &x in &nums {
16+
if let Some(v) = right.get_mut(&x) {
17+
*v -= 1;
18+
}
19+
20+
let t = x * 2;
21+
22+
let l = *left.get(&t).unwrap_or(&0);
23+
let r = *right.get(&t).unwrap_or(&0);
24+
25+
ans = (ans + (l * r) % modulo) % modulo;
26+
27+
*left.entry(x).or_insert(0) += 1;
28+
}
29+
30+
ans as i32
31+
}
32+
}

0 commit comments

Comments
 (0)