-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.twoSum.cpp
More file actions
29 lines (28 loc) · 913 Bytes
/
1.twoSum.cpp
File metadata and controls
29 lines (28 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* @lc app=leetcode id=1 lang=cpp
*
* [1] Two Sum
*/
#include <vector>
#include <unordered_map>
using namespace std;
// @lc code=start
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
// map用红黑树实现,适合有序的情况
// unordered_map用Hash实现,适合查找
unordered_map<int, int> indices;
for (int i = 0; i < nums.size(); i++) {
if (indices.find(target - nums[i]) != indices.end()) {
// 这玩意就是个用列表初始化构造了一个vector,放在return死活没认出来
return {indices[target - nums[i]], i};
}
// 先查找再添加本次值,避免重复查值(6=3+3)的情况
// 根据值查索引,所以Key=数组值,Value=数组索引
indices[nums[i]] = i;
}
return {};
}
};
// @lc code=end