-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0001_two_sum.rs
More file actions
35 lines (30 loc) · 834 Bytes
/
s0001_two_sum.rs
File metadata and controls
35 lines (30 loc) · 834 Bytes
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
#![allow(unused)]
pub struct Solution {}
use std::collections::HashMap;
impl Solution {
// O(n) O(n)
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut ans = vec![];
let mut map = HashMap::new();
for k in 0..nums.len() {
if !map.contains_key(&nums[k]) {
map.insert(target - nums[k], k);
} else {
ans.push(*map.get(&nums[k]).unwrap() as i32);
ans.push(k as i32);
return ans;
}
}
ans
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), [0, 1]);
assert_eq!(Solution::two_sum(vec![3, 2, 4], 6), [1, 2]);
assert_eq!(Solution::two_sum(vec![3, 3], 6), [0, 1]);
}
}