We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c20038f commit db99fe1Copy full SHA for db99fe1
algorithms/cpp/twoSum/twoSum.cpp
@@ -66,4 +66,19 @@ class Solution {
66
}
67
return result;
68
69
+
70
+ // we also can store nums[i] into map, and find target - nums[i]
71
+ vector<int> twoSum(vector<int>& nums, int target) {
72
+ unordered_map<int, int> m;
73
+ vector<int> result;
74
+ for (int i=0; i<nums.size(); i++) {
75
+ if ( m.find(target - nums[i]) == m.end() ) {
76
+ m[nums[i]] = i;
77
+ }else{
78
+ result.push_back(m[target - nums[i]]);
79
+ result.push_back(i);
80
+ }
81
82
+ return result;
83
84
};
0 commit comments