Skip to content

Instantly share code, notes, and snippets.

@ByungSunBae
Created January 29, 2020 09:50
Show Gist options
  • Save ByungSunBae/71aa449ed6b4b4d0f8a3d332fee90b46 to your computer and use it in GitHub Desktop.
Save ByungSunBae/71aa449ed6b4b4d0f8a3d332fee90b46 to your computer and use it in GitHub Desktop.
two integers in rust
//use std::cmp;
/*
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
}
}
*/
fn main() {
let vec = vec![100, 99, 98, 97, 30];
let target = 130;
two_sum(vec, target)
}
fn two_sum(nums: Vec<i32>, target: i32) {
let mut idx: i32 = 0;
for num in nums.iter() {
if target > *num {
let diff = target - num;
if nums.iter().any(|x| *x == diff) {
println!("the two numbers: {}, {}", num, diff);
let idx2 = nums.iter().position(|x| *x == diff);
println!("indicies: {}, {}", idx, idx2.unwrap_or_default());
break
}
//println!("the difference and index: {}, {}", diff, idx);
}
idx += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment