forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_sum.rs
39 lines (31 loc) · 967 Bytes
/
two_sum.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::collections::HashMap;
use std::convert::TryInto;
// Given an array of integers nums and an integer target,
// return indices of the two numbers such that they add up to target.
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut hash_map: HashMap<i32, i32> = HashMap::new();
for (i, item) in nums.iter().enumerate() {
match hash_map.get(&(target - item)) {
Some(value) => {
return vec![i.try_into().unwrap(), *value];
}
None => {
hash_map.insert(*item, i.try_into().unwrap());
}
}
}
vec![]
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test() {
let nums = vec![2, 7, 11, 15];
assert_eq!(two_sum(nums, 9), vec![1, 0]);
let nums = vec![3, 2, 4];
assert_eq!(two_sum(nums, 6), vec![2, 1]);
let nums = vec![3, 3];
assert_eq!(two_sum(nums, 6), vec![1, 0]);
}
}