-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_sum.rs
More file actions
34 lines (28 loc) · 1.06 KB
/
two_sum.rs
File metadata and controls
34 lines (28 loc) · 1.06 KB
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
// Require
use std::collections::HashMap;
// Solution function
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
/*
Instantiate the map between the required value
to reach the target and the index of the complement
*/
let mut solve_hash: HashMap<i32, i32> = HashMap::new();
// Loop over the provided array and search for the solution
for (index, value) in nums.iter().enumerate() {
// Convert the index as i32 using idiomatic "as"
let index_32: i32 = index as i32;
// Check if the solve_hash already has the complement
let has_compl: bool = solve_hash.contains_key(value);
// If the value has a complement, return the values indexes
if has_compl {
// Get the compement value
let compl_index = solve_hash.get(value).unwrap();
return vec![*compl_index, index_32];
}
// Else, add the key as the complement
let complement = target - value;
solve_hash.insert(complement, index_32);
}
// Base return
return Vec::new();
}