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/src/main.rs | |
lc starting today - 2025-11-30
Diffstat (limited to '01_two-sum/src/main.rs')
| -rw-r--r-- | 01_two-sum/src/main.rs | 26 |
1 files changed, 26 insertions, 0 deletions
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 + } +} |
