Skip to content

Commit

Permalink
no tricky solution
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Mar 29, 2019
1 parent c20038f commit db99fe1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions algorithms/cpp/twoSum/twoSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,19 @@ class Solution {
}
return result;
}

// we also can store nums[i] into map, and find target - nums[i]
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
vector<int> result;
for (int i=0; i<nums.size(); i++) {
if ( m.find(target - nums[i]) == m.end() ) {
m[nums[i]] = i;
}else{
result.push_back(m[target - nums[i]]);
result.push_back(i);
}
}
return result;
}
};

0 comments on commit db99fe1

Please sign in to comment.