From 4b65ece9ed30a88962782f1742d2d07d53783010 Mon Sep 17 00:00:00 2001 From: "Mr.D" Date: Sun, 26 Mar 2023 16:52:18 +0800 Subject: [PATCH] Add another Rust solution --- problems/0001.两数之和.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index b3abb991..eea3ba7a 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -216,6 +216,26 @@ impl Solution { } } ``` +Rust + +``` +use std::collections::HashMap; + +impl Solution { + pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut hm: HashMap = HashMap::new(); + for i in 0..nums.len() { + let j = target - nums[i]; + if hm.contains_key(&j) { + return vec![*hm.get(&j).unwrap(), i as i32] + } else { + hm.insert(nums[i], i as i32); + } + } + vec![-1, -1] + } +} +``` Javascript