-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_sum.rs
61 lines (49 loc) · 1.42 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::collections::HashMap;
/// Given an array of integers `nums` and an integer `target`, return indices
/// of the two numbers such that they add up to `target`.
///
/// You may assume that each input would have exactly one solution, and you may
/// not use the same element twice.
///
/// You can return the answer in any order.
struct Solution;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut results = vec![0, 0];
let mut indices: HashMap<i32, Vec<usize>> = HashMap::new();
let n = nums.len();
for i in 0..n {
let num = nums[i];
let diff = target - num;
if indices.contains_key(&diff) {
results[0] = indices[&diff][0] as i32;
results[1] = i as i32;
break;
}
indices
.entry(num)
.or_insert(Vec::new())
.push(i);
}
results
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_1() {
let result1 = Solution::two_sum(vec![2, 7, 11, 15], 9);
assert_eq!(result1, vec![0, 1]);
}
#[test]
fn example_2() {
let result2 = Solution::two_sum(vec![3, 2, 4], 6);
assert_eq!(result2, vec![1, 2]);
}
#[test]
fn example_3() {
let result3 = Solution::two_sum(vec![3, 3], 6);
assert_eq!(result3, vec![0, 1]);
}
}