-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_sum.rs
60 lines (48 loc) · 1.3 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
#![allow(dead_code)]
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 map.contains_key(&complement) {
return vec![*map.get(&complement).unwrap() as i32, i as i32];
}
map.insert(num, i);
}
vec![]
}
/*
Topic: array, hash table
Algorithm - One-pass Hash Table
- Iterate through each element in nums
- Check if target - nums[i] exists in the hash table
- If it does, return the indices
- If it doesn't, insert nums[i] in the hash table
Time Complexity: O(n)
Space Complexity: O(n)
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let nums = vec![2, 7, 11, 15];
let target = 9;
let result = two_sum(nums, target);
assert_eq!(result, vec![0, 1]);
}
#[test]
fn test_2() {
let nums = vec![3, 2, 4];
let target = 6;
let result = two_sum(nums, target);
assert_eq!(result, vec![1, 2]);
}
#[test]
fn test_3() {
let nums = vec![3, 3];
let target = 6;
let result = two_sum(nums, target);
assert_eq!(result, vec![0, 1]);
}
}