Created
October 5, 2021 12:37
-
-
Save darotar/41bd1aa0fbfbf8b49eab1541353ee732 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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