mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
增加0454.四数相加II Rust語言版本
Note: - leetcode上提交通過
This commit is contained in:
@ -282,6 +282,31 @@ func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
impl Solution {
|
||||
pub fn four_sum_count(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>, nums4: Vec<i32>) -> i32 {
|
||||
let mut umap:HashMap<i32, i32> = HashMap::new();
|
||||
for num1 in &nums1 {
|
||||
for num2 in &nums2 {
|
||||
*umap.entry(num1 + num2).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
for num3 in &nums3 {
|
||||
for num4 in &nums4 {
|
||||
let target:i32 = - (num3 + num4);
|
||||
count += umap.get(&target).unwrap_or(&0);
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
Reference in New Issue
Block a user