Skip to content

Instantly share code, notes, and snippets.

@darotar
Created October 5, 2021 12:37
Show Gist options
  • Save darotar/41bd1aa0fbfbf8b49eab1541353ee732 to your computer and use it in GitHub Desktop.
Save darotar/41bd1aa0fbfbf8b49eab1541353ee732 to your computer and use it in GitHub Desktop.
fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut map: HashMap<i32, i32> = HashMap::new();
for (i, num) in nums.iter().enumerate() {
let complement: i32 = target - nums[i];
let i_i32 = i as i32;
if map.contains_key(&complement) {
let map_complement = *map.get(&complement).unwrap();
return vec![map_complement, i_i32];
}
map.insert(*num, i_i32);
}
vec![]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment