-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.3-sum.java
134 lines (130 loc) · 4.11 KB
/
15.3-sum.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*
* @lc app=leetcode id=15 lang=java
*
* [15] 3Sum
*
* https://leetcode.com/problems/3sum/description/
*
* algorithms
* Medium (25.33%)
* Likes: 5070
* Dislikes: 613
* Total Accepted: 722.3K
* Total Submissions: 2.8M
* Testcase Example: '[-1,0,1,2,-1,-4]'
*
* Given an array nums of n integers, are there elements a, b, c in nums such
* that a + b + c = 0? Find all unique triplets in the array which gives the
* sum of zero.
*
* Note:
*
* The solution set must not contain duplicate triplets.
*
* Example:
*
*
* Given array nums = [-1, 0, 1, 2, -1, -4],
*
* A solution set is:
* [
* [-1, 0, 1],
* [-1, -1, 2]
* ]
*
*/
// @lc code=start
// class Solution {
// public List<List<Integer>> threeSum(int[] nums) {
// List<Integer> negativeNums = new ArrayList<Integer>();
// List<Integer> positiveNums = new ArrayList<Integer>();
// int zeroCount = 0;
// List<List<Integer>> results = new ArrayList<List<Integer>>();
// for (int i = 0; i < nums.length; i++) {
// if (nums[i] > 0)
// positiveNums.add(nums[i]);
// else if (nums[i] == 0)
// zeroCount++;
// else
// negativeNums.add(nums[i]);
// }
// Set<Integer> negativeSets = new HashSet<>(negativeNums);
// Set<Integer> positiveSets = new HashSet<>(positiveNums);
// for (int i : negativeSets) {
// results.addAll(twoSum(positiveNums, -i));
// }
// for (int i : positiveSets) {
// results.addAll(twoSum(negativeNums, -i));
// }
// if (zeroCount > 0) {
// for (int i : negativeSets) {
// if (positiveSets.contains(-i)) {
// results.add(Arrays.asList(i, 0, -i));
// }
// }
// if (zeroCount >= 3) {
// results.add(Arrays.asList(0, 0, 0));
// }
// }
// return results;
// }
// public List<List<Integer>> twoSum(List<Integer> nums, int value) {
// List<List<Integer>> results = new ArrayList<List<Integer>>();
// Set<Integer> sets = new HashSet<>();
// Set<Integer> addSets = new HashSet<>();
// for (int i = 0; i < nums.size(); i++) {
// final int tempInt = nums.get(i);
// int target = value - tempInt;
// if (sets.contains(target) && !addSets.contains(nums.get(i))) {
// addSets.add(tempInt);
// addSets.add(target);
// results.add(Arrays.asList(tempInt, target, -value));
// } else {
// sets.add(tempInt);
// }
// }
// return results;
// }
// }
// A faster version: used two pointer while computing two sum results
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> results = new ArrayList<>();
if (nums.length < 3)
return results;
for (int i = 0; i < nums.length; i++) {
int target = -nums[i];
if (nums[i] > 0)
break;
if (i > 0 && nums[i] == nums[i - 1])
continue;
int low = i + 1;
int high = nums.length - 1;
// computing two sum results
while (low < high) {
int twoSum = nums[low] + nums[high];
if (twoSum == target) {
results.add(Arrays.asList(-target, nums[low], nums[high]));
while (low < high && nums[low] == nums[++low])
;
while (low < high && nums[high] == nums[--high])
;
} else if (twoSum < target)
low++;
else
high--;
}
}
return results;
}
}
// @lc code=end
// [-1, 0, 1, 2, -1, -4, 2]
// [-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]
// [-4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0]