-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtwo_sum.cpp
More file actions
46 lines (42 loc) · 1.07 KB
/
two_sum.cpp
File metadata and controls
46 lines (42 loc) · 1.07 KB
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
43
44
45
46
//
// Created by roseduan on 2021/9/18.
//
#include "iostream"
#include "vector"
#include "unordered_map"
using namespace std;
class Solution{
public:
// 1. 直接遍历
vector<int> twoSum1(vector<int>& nums, int target) {
for (int i = 0; i < nums.size() - 1; ++i) {
for (int j = i + 1; j < nums.size(); ++j) {
if (nums[i] + nums[j] == target) {
return {i, j};
}
}
}
return {};
}
// 1. 使用一个哈希表
vector<int> twoSum2(vector<int>& nums, int target) {
unordered_map<int, int> table;
for (int i = 0; i < nums.size(); ++i) {
auto it = table.find(target - nums[i]);
if (it != table.end()) {
return {it->second, i};
}
table[nums[i]] = i;
}
return {};
}
};
int main() {
cout << "Hello World" << endl;
Solution sol;
vector<int> data {1, 5, 3, 9};
const vector<int> &res = sol.twoSum2(data, 4);
for (auto i : res) {
cout << i << endl;
}
}