From ac4fec799bebbe2cbabf8ee54a73ea89680dad6c Mon Sep 17 00:00:00 2001 From: YiChih Wang Date: Sun, 12 Sep 2021 16:27:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A00454.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E7=9B=B8=E5=8A=A0II=20Rust=E8=AA=9E=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: - leetcode上提交通過 --- problems/0454.四数相加II.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index a82c617b..475f93a0 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -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, nums2: Vec, nums3: Vec, nums4: Vec) -> i32 { + let mut umap:HashMap = 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)