-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-TwoSum.cc
42 lines (41 loc) · 1.08 KB
/
1-TwoSum.cc
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
35
36
37
38
39
40
41
42
// 暴力枚举,复杂度O(n^2)
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
int sum = target;
for (int i = 0; i < nums.size(); i++)
{
for (int j = i + 1; j < nums.size(); j++)
{
if (target == nums[i] + nums[j])
return vector<int>{i, j};
}
}
return vector<int>{0, 0};
}
};
// 哈希表,存储每个数对应的下标,复杂度O(n)
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
unordered_map<int, int> map;
vector<int> result;
for (int i = 0; i < nums.size(); i++)
map[nums[i]] = i; // 用哈希表存储每个数对应下标
for (int i = 0; i < nums.size(); i++)
{
const int gap = target - nums[i];
if (map.find(gap) != map.end() && map[gap] > i)
{
result.push_back(i);
result.push_back(map[gap]);
break;
}
}
return result;
}
};