Loading RustViz Engine...
Find two numbers in an array that add up to a target using std::collections::HashMap.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
use std::collections::HashMap;
pub fn two_sum(nums: &[i32], target: i32) -> Option<(usize, usize)> {
let mut map = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&prev_idx) = map.get(&complement) {
return Some((prev_idx, i));
}
map.insert(num, i);
}
None
}