diff options
Diffstat (limited to '0001')
| -rw-r--r-- | 0001/Cargo.lock | 7 | ||||
| -rw-r--r-- | 0001/Cargo.toml | 6 | ||||
| -rw-r--r-- | 0001/src/main.rs | 25 |
3 files changed, 38 insertions, 0 deletions
diff --git a/0001/Cargo.lock b/0001/Cargo.lock new file mode 100644 index 0000000..cf4788e --- /dev/null +++ b/0001/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "p1" +version = "0.1.0" diff --git a/0001/Cargo.toml b/0001/Cargo.toml new file mode 100644 index 0000000..47a9ccd --- /dev/null +++ b/0001/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "p1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/0001/src/main.rs b/0001/src/main.rs new file mode 100644 index 0000000..70128c9 --- /dev/null +++ b/0001/src/main.rs @@ -0,0 +1,25 @@ +use std::collections::HashMap; + +struct Solution {} +impl Solution { + pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { + let mut num_map : HashMap<i32, usize> = HashMap::new(); + + let mut i:usize = 0; + loop { + let index = target - nums[i]; + match num_map.get(&index) { + Some(j) => break vec![i as i32, *j as i32], + None => { + num_map.insert(nums[i], i); + i = i + 1; + continue; + } + } + } + } +} + +fn main() { + println!("{:?}", Solution::two_sum(vec![2, 7, 11, 15], 9)); +}
\ No newline at end of file |
