From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- two-sum/src/main.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 two-sum/src/main.rs (limited to 'two-sum/src/main.rs') diff --git a/two-sum/src/main.rs b/two-sum/src/main.rs new file mode 100644 index 0000000..8b18400 --- /dev/null +++ b/two-sum/src/main.rs @@ -0,0 +1,28 @@ +fn main() { + println!("Hello, world!"); + let tests = [ + (vec![2,7,11,15], 9, vec![0,1]), + (vec![3,2,4], 6, vec![1,2]), + (vec![3,3], 6, vec![0,1]) + ]; + + for test in tests { + println!("{:?} at {} is {:?} should be {:?}", + test.0, + test.1, + two_sum(test.0.clone(), test.1), + test.2 + ); + } +} + +pub fn two_sum(nums: Vec, target: i32) -> Vec { + for i in 0..nums.len()-1 { + for ii in i+1..nums.len() { + if nums[i] + nums[ii] == target { + return vec![i as i32, ii as i32]; + } + } + } + return vec![0,0]; +} -- cgit v1.2.3