-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.两数之和.cpp
More file actions
35 lines (30 loc) · 763 Bytes
/
1.两数之和.cpp
File metadata and controls
35 lines (30 loc) · 763 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
29
30
31
32
33
34
/*
* @lc app=leetcode.cn id=1 lang=cpp
*
* [1] 两数之和
*/
// @lc code=start
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> diction; // 辅助字典
vector<int> result; //
for (int i = 0; i < nums.size(); i++)
{
int middle = target - nums.at(i);
auto iter = diction.find(nums.at(i));
if (iter == diction.end())
{
diction.insert(pair<int, int> (middle, i));
}
else
{
result.push_back(iter->second); //
result.push_back(i);
break;
}
}
return result;
}
};
// @lc code=end