LeetCode/solutions/1. Two Sum.md

26 lines
604 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [1. Two Sum](https://leetcode.com/problems/two-sum/)
# 思路
刚开始用暴力匹配后来看了答案恍然大悟hash会快很多。
# C++
``` C++
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;
}
}
};
```