From 6363aeaed3abea07a25ce5ae21594f32021ebe05 Mon Sep 17 00:00:00 2001 From: Wander_Lust Date: Sat, 6 Dec 2025 10:00:23 +0000 Subject: [PATCH] Two-sum solution --- leetcode/two-sum/TwoPass.cpp | 16 ++++++++++++++++ leetcode/two-sum/brute.cpp | 13 +++++++++++++ leetcode/two-sum/notes.md | 15 +++++++++++++++ leetcode/two-sum/onePass.cpp | 13 +++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 leetcode/two-sum/TwoPass.cpp create mode 100644 leetcode/two-sum/brute.cpp create mode 100644 leetcode/two-sum/notes.md create mode 100644 leetcode/two-sum/onePass.cpp diff --git a/leetcode/two-sum/TwoPass.cpp b/leetcode/two-sum/TwoPass.cpp new file mode 100644 index 0000000..89a1c13 --- /dev/null +++ b/leetcode/two-sum/TwoPass.cpp @@ -0,0 +1,16 @@ +class Solution{ +public: + vector twoSum(vector &nums, int target) { + unordered_map hash; + for (int i = 0; i < nums.size(); i++) { + hash[nums[i]] = i; + } + for (int i = 0; i < nums.size(); i++) { + int comp = target - nums[i]; + if (hash.find(comp) != hash.end() && hash[comp] != i) { + return {i, hash[comp]}; + } + } + return {}; + } +}; diff --git a/leetcode/two-sum/brute.cpp b/leetcode/two-sum/brute.cpp new file mode 100644 index 0000000..903a3a6 --- /dev/null +++ b/leetcode/two-sum/brute.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + vector twoSum(vector& nums, int target) { + for(int i=0;i twoSum(vector& nums, int target){ + unordered_maphash; + for(int i=0;i