Add another Rust solution

This commit is contained in:
Mr.D
2023-03-26 16:52:18 +08:00
parent 2cf0b654e5
commit 4b65ece9ed

View File

@ -216,6 +216,26 @@ impl Solution {
}
}
```
Rust
```
use std::collections::HashMap;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut hm: HashMap<i32, i32> = 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