From a5e3b1140ec34399e172e0405eb99f323e275a24 Mon Sep 17 00:00:00 2001 From: m4siri Date: Sun, 30 Nov 2025 20:47:11 +0545 Subject: lc starting today - 2025-11-30 --- 01_two-sum/Cargo.lock | 7 +++++++ 01_two-sum/Cargo.toml | 6 ++++++ 01_two-sum/src/main.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 01_two-sum/Cargo.lock create mode 100644 01_two-sum/Cargo.toml create mode 100644 01_two-sum/src/main.rs (limited to '01_two-sum') 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 = 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, target: i32) -> Vec { + 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 + } +} -- cgit v1.2.3