Two Sum ⧉
_20use std::collections::HashMap;_20_20impl Solution {_20 pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {_20 let mut m = HashMap::<i32,i32>::new();_20 for (i, v) in nums.iter().enumerate() {_20 m.insert(v.clone(), i as i32);_20 }_20 for (idx, v) in nums.iter().enumerate() {_20 let i = idx as i32;_20 let rest = target-v;_20 if (m.contains_key(&rest)) {_20 if i != *m.get(&rest).unwrap() {_20 return vec![i, *m.get(&rest).unwrap()];_20 }_20 }_20 }_20 vec![-1, -1]_20 }_20}
If you found this solution helpful, consider leaving a star!