mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
Add another Rust solution
This commit is contained in:
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user