1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
    let map: HashMap<i32, usize> = nums.iter().enumerate().map(|(idx, &data)| (data, idx)).collect();

    nums.iter().enumerate().find(|(idx, &num)| {
        match  map.get(&(target - num)) {
            Some(&idx_in_map) => idx_in_map != *idx,
            None => false,
        }
    }).map(|(idx, &num)| vec![*map.get(&(target - num)).unwrap() as i32, idx as i32]).unwrap()

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
pub fn two_sum_v2(nums: Vec<i32>, target: i32) -> Vec<i32> {
    let map: HashMap<i32, usize> = nums.iter().enumerate().map(|(idx, &data)| (data, idx)).collect();

    for (i,&num) in nums.iter().enumerate() {
        match map.get(&(target - num) ) {
            Some(&x) => {
                if i != x {
                    return vec![i as i32, x as i32]
                }
            },
            None => continue,
        }
    }
    vec![]
}