-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_sum.cpp
More file actions
63 lines (49 loc) · 1.55 KB
/
3_sum.cpp
File metadata and controls
63 lines (49 loc) · 1.55 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* leetcode题目,给定一个数组,找出所有之和为0的三个元素
* Given an array S of n integers, are there elements a,b,c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
• Elements in a triplet (a, b, c) must be in non-descending order. (ie, a ≤ b ≤ c) • Thesolutionsetmustnotcontainduplicatetriplets.
For example, given array S = {-1 0 1 2 -1 -4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
*/
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <iostream>
using namespace std;
void three_sum(vector<int> in)
{
unordered_map<int, int> nums;
//sort(in.begin(), in.end());
int sum = 0;
int minus = 0;
//先建立一个以数组元素值为key的hash表
for(int i = 0; i < in.size(); i++) {
nums[in[i]] = i + 1;
}
for(int i = 0; i < in.size(); i++) {
for(int j = i + 1; j < in.size(); j++) {
//cout<<"i "<<i<<" j "<<j<<endl;
sum = in[i] + in[j];
minus = 0 - sum;
//cout<<"minus "<<minus<<" "<<nums[minus]<<endl;
if (nums[minus]) //找到key
{
int key = nums[minus] - 1;
if ((key > i) && (key > j)) {
cout<<"i "<<i<<" j "<<j<<" key "<<key<<endl;
cout<<"the numer: "<<in[i]<<" "<<in[j]<<" "<<in[key]<<endl;
}
}
}
}
}
int main()
{
int n[] = {-1, 0, 1, 2, -1, -4} ;
vector<int> test(n, n + 6);
three_sum(test);
return 0;
}