Skip to content

Commit 4d43cb6

Browse files
authored
Merge pull request #2348 from DaleStudy/week1
[DaleSeo] WEEK 1 Solutions
2 parents 1affa63 + 8219550 commit 4d43cb6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

two-sum/DaleSeo.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::collections::HashMap;
2+
3+
// TC: O(n)
4+
// SC: O(n)
5+
impl Solution {
6+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
7+
let mut indices = HashMap::new();
8+
for (i, &num) in nums.iter().enumerate() {
9+
let complement = target - num;
10+
if let Some(&j) = indices.get(&complement) {
11+
return vec![j as i32, i as i32];
12+
}
13+
indices.insert(num, i);
14+
}
15+
vec![]
16+
}
17+
}

0 commit comments

Comments
 (0)