add 001-Two Sum

This commit is contained in:
唐树森 2018-07-15 20:57:01 +08:00
parent 6f637fba23
commit 6dad930360
2 changed files with 20 additions and 0 deletions

4
001-Two Sum/README.md Normal file
View File

@ -0,0 +1,4 @@
LeetCode第一个题
# C++
刚开始用暴力匹配后来看了答案恍然大悟hash会快很多

16
001-Two Sum/solution.h Normal file
View File

@ -0,0 +1,16 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int>mp;
vector<int>ans;
int len = nums.size();
for(int i = 0; i < len; i++){
if( mp.find(target - nums[i]) != mp.end()){
ans.push_back(mp[target - nums[i]]);
ans.push_back(i);
return ans;
}
mp[nums[i]] = i;
}
}
};