-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathleetcode15_3sum.cpp
More file actions
87 lines (73 loc) · 2.06 KB
/
leetcode15_3sum.cpp
File metadata and controls
87 lines (73 loc) · 2.06 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @file leetcode15_3sum.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2021/12/07 20:46:39
* @brief
*
**/
#include <iostream>
#include <vector>
#include <algorithm>
class Solution {
public:
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
std::vector<std::vector<int>> ret_arrays;
std::sort(nums.begin(), nums.end());
for (size_t i = 0; i < nums.size(); ++i) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
size_t j = i + 1;
size_t k = nums.size() - 1;
while (j < k) {
int two_sum = nums[j] + nums[k];
if (two_sum == (0 - nums[i])) {
std::vector<int> pick;
pick.push_back(nums[i]);
pick.push_back(nums[j]);
pick.push_back(nums[k]);
ret_arrays.push_back(pick);
while ((++j < k) && (nums[j] == nums[j-1])) {}
while ((j < --k) && (nums[k] == nums[k+1])) {}
} else if (two_sum > (0 - nums[i])) {
--k;
} else {
++j;
}
}
}
return ret_arrays;
}
};
int main() {
int n;
std::vector<int> nums;
while (1) {
n = 0;
std::cout << "Number of elements: ";
std::cin >> n;
if (n <= 0) {
return 0;
}
std::cout << n << " integers: " << std::endl;
nums.clear();
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
nums.push_back(ele);
}
std::vector<std::vector<int>> ret_arrays;
Solution solution;
ret_arrays = solution.threeSum(nums);
std::cout << "[";
for (auto arr: ret_arrays) {
std::cout << "[";
for (auto x: arr) {
std:: cout << x << " ";
}
std::cout << "]";
}
std::cout << "]" << std::endl;
}
return 0;
}