diff options
| author | m4siri <git@m4siri.com> | 2025-11-30 20:47:11 +0545 |
|---|---|---|
| committer | m4siri <git@m4siri.com> | 2025-11-30 20:50:52 +0545 |
| commit | a5e3b1140ec34399e172e0405eb99f323e275a24 (patch) | |
| tree | d3b436ed072a86e53d3fae7a2ebdf35a7337f19f /01_two-sum | |
lc starting today - 2025-11-30
Diffstat (limited to '01_two-sum')
| -rw-r--r-- | 01_two-sum/Cargo.lock | 7 | ||||
| -rw-r--r-- | 01_two-sum/Cargo.toml | 6 | ||||
| -rw-r--r-- | 01_two-sum/src/main.rs | 26 |
3 files changed, 39 insertions, 0 deletions
diff --git a/01_two-sum/Cargo.lock b/01_two-sum/Cargo.lock new file mode 100644 index 0000000..b32bc1e --- /dev/null +++ b/01_two-sum/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "two_sum" +version = "0.1.0" diff --git a/01_two-sum/Cargo.toml b/01_two-sum/Cargo.toml new file mode 100644 index 0000000..1ee142e --- /dev/null +++ b/01_two-sum/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "two_sum" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/01_two-sum/src/main.rs b/01_two-sum/src/main.rs new file mode 100644 index 0000000..827e46d --- /dev/null +++ b/01_two-sum/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + let nums: Vec<i32> = vec![2, 7, 11, 15]; + let target: i32 = 9; + dbg!(Solution::two_sum(nums, target)); +} + +struct Solution(()); +impl Solution { + pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { + let mut result = Vec::new(); + let mut idx = 0; + + while idx < nums.len() { + let cur = nums[idx]; + let offset = idx + 1; + for (i, num) in nums[offset..].into_iter().enumerate() { + if (cur + num) == target { + result.push(idx as i32); + result.push((offset + i) as i32); + } + } + idx += 1; + } + result + } +} |
