leecode 1
两数之和:
- 暴力法 O(N^2)
- hashmap法 O(N)
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { // unordered_map<int,int> hashmap; map<int,int> hashmap; for (int i = 0; i < nums.size(); i++) { auto it = hashmap.find(target - nums[i]); if (it != hashmap.end()) { return {i,it->second}; } hashmap[nums[i]] = i; } return {-1,-1}; } };
知识点解析: - 哈希表,数据结构,以及key和value的访问机制
- 迭代器的使用
- unordered_map和map的区别


