O(n) using hashmap
| 12 | public: |
| 13 | // O(n) using hashmap |
| 14 | vector<int> twoSum(vector<int>& nums, int target) { |
| 15 | unordered_map<int, int> map; |
| 16 | for (int i=0; i<nums.size(); i++) { |
| 17 | auto x = map.find(target - nums[i]); |
| 18 | if (x != map.end()) return {x->second, i}; |
| 19 | map[nums[i]] = i; |
| 20 | } |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | int main() { |