From 7e5083a7057d5a5a12fc749867c9e207a0a211ed Mon Sep 17 00:00:00 2001 From: Emar Ali <44610462+emaaarrr@users.noreply.github.com> Date: Fri, 1 Oct 2021 19:44:43 +0530 Subject: [PATCH] Create Two sums improved --- Two sums imporved | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Two sums imporved diff --git a/Two sums imporved b/Two sums imporved new file mode 100644 index 00000000..2a26a0fc --- /dev/null +++ b/Two sums imporved @@ -0,0 +1,21 @@ +class Solution +{ +public: + vector twoSum(vector &nums, int target) + { + vector ans = {}; + unordered_map hashMap; + for (int i = 0; i < nums.size(); i++) + { + if (hashMap.find(target - nums[i]) != hashMap.end()) + { + ans.push_back(hashMap[target - nums[i]]); + ans.push_back(i); + return ans; + } + hashMap[nums[i]] = i; + } + return ans; + } +}; +// https://leetcode.com/problems/two-sum/discuss/?currentPage=1&orderBy=hot&query=