diff options
author | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
commit | 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch) | |
tree | 0072cca328fe5adb2ed61004010228ff85e2164d /two-sum |
Diffstat (limited to 'two-sum')
-rw-r--r-- | two-sum/Cargo.toml | 8 | ||||
-rw-r--r-- | two-sum/src/main.rs | 28 |
2 files changed, 36 insertions, 0 deletions
diff --git a/two-sum/Cargo.toml b/two-sum/Cargo.toml new file mode 100644 index 0000000..540b825 --- /dev/null +++ b/two-sum/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "two-sum" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] 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<i32>, target: i32) -> Vec<i32> { + 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]; +} |