- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha0001_two_sum.rs
44 lines (35 loc) · 921 Bytes
/
a0001_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
40
41
42
43
44
/*
* [0001] two-sum
*/
pubstructSolution{}
// solution impl starts here
use std::collections::HashMap;
implSolution{
pubfntwo_sum(nums:Vec<i32>,target:i32) -> Vec<i32>{
letmut map = HashMap::with_capacity(nums.len());
for(index, num)in nums.iter().enumerate(){
match map.get(&(target - num)){
None => {
map.insert(num, index);
}
Some(sub_index) => returnvec![*sub_index asi32, index asi32],
}
}
vec![]
}
}
// solution impl ends here
// solution tests starts here
#[cfg(test)]
mod tests {
usesuper::*;
#[test]
fntest_case0(){
assert_eq!(vec![0,1],Solution::two_sum(vec![2,7,11,15],9));
}
#[test]
fntest_case1(){
assert_eq!(vec![1,2],Solution::two_sum(vec![3,2,4],6));
}
}
// solution tests ends here