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=