-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rs
More file actions
30 lines (26 loc) · 711 Bytes
/
main.rs
File metadata and controls
30 lines (26 loc) · 711 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
fn main() {
assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), [0, 1]);
}
struct Solution {}
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
use std::collections::HashMap;
let mut map: HashMap<i32, i32> = HashMap::new();
for i in 0..nums.len() {
let pair = target - nums[i];
match map.get(&pair) {
None => { map.insert(nums[i], i as i32); }
Some(&idx) => { return vec![idx, i as i32] }
}
}
unimplemented!()
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), [0, 1]);
}
}