pull down to refresh
use std::collections::HashMap; pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { let mut map = HashMap::new(); for (i, &num) in nums.iter().enumerate() { let complement = target - num; if let Some(&index) = map.get(&complement) { return vec![index as i32, i as i32]; } map.insert(num, i); } vec![] } #[cfg(test)] mod tests { use super::*; #[test] fn test_example_1() { let nums = vec![1, 8, 10, 21]; let target = 18; assert_eq!(two_sum(nums, target), vec![1, 2]); } #[test] fn test_example_2() { let nums = vec![5, 12, 19, 3]; let target = 8; assert_eq!(two_sum(nums, target), vec![0, 3]); } #[test] fn test_example_3() { let nums = vec![6, 6]; let target = 12; assert_eq!(two_sum(nums, target), vec![0, 1]); } }