Last active
May 25, 2020 09:48
-
-
Save ridv/5bd33d1cd2786b20b2f7f5ebe4f8a5be 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
use std::collections::HashMap; | |
impl Solution { | |
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { | |
let mut lookup = HashMap::new(); | |
for i in 0..nums.len() { | |
let key = &(target- nums[i]); | |
if lookup.contains_key(key) { | |
return vec![*lookup.get(key).unwrap(), (i as i32)]; | |
} | |
lookup.insert(nums[i], (i as i32)); | |
} | |
return vec![]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment