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