Skip to content

Commit a635756

Browse files
committed
two-sum
1 parent 53ce7c7 commit a635756

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

two-sum/livemehere.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+
impl Solution {
4+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
5+
let mut map: HashMap<i32, i32> = HashMap::new();
6+
7+
for (idx, &n) in nums.iter().enumerate() {
8+
let pair = target - n;
9+
if let Some(&pair_idx) = map.get(&pair) {
10+
return vec![idx as i32, pair_idx];
11+
} else {
12+
map.insert(n, idx as i32);
13+
}
14+
}
15+
vec![]
16+
}
17+
}

0 commit comments

Comments
 (0)