daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::collections::HashMap;
  2.  
  3. pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
  4.     let mut visited = HashMap::with_capacity(nums.len());
  5.  
  6.     for (num, i) in nums.into_iter().zip(0..) {
  7.         let pair = target - num;
  8.         match visited.get(&pair) {
  9.             Some(j) => return vec![i, *j],
  10.             None => {
  11.                 visited.insert(num, i);
  12.             },
  13.         }
  14.     }
  15.  
  16.     panic!("no solution")
  17. }
  18.  
  19.  
  20. fn main() {
  21.     dbg!(two_sum(vec![2, 7, 11, 15], 9));
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top