Repository navigation
Expand file tree
/
Copy pathThreeSum.java
More file actions
69 lines (63 loc) · 2.09 KB
/
Copy pathThreeSum.java
File metadata and controls
69 lines (63 loc) · 2.09 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
class Solution {
// Given an integer array nums, return all unique triplets
// [nums[i], nums[j], nums[k]] such that i != j != k
// and nums[i] + nums[j] + nums[k] == 0. The solution set
// must not contain duplicate triplets.
// Example:
// Input : nums = [-1, 0, 1, 2, -1, -4]
// Output : [[-1, -1, 2], [-1, 0, 1]]
// Explanation: The unique triplets that sum to zero are [-1, -1, 2] and [-1, 0,
// 1].
//
//
// 1. Iterate nums array, while using two pointers
public static List<List<Integer>> tripletsWithSum0(List<Integer> nums) {
Collections.sort(nums);
List<List<Integer>> result = new ArrayList<>();
int n = nums.size();
for (int i = 0; i < n; i++) {
if (nums.get(i) > 0)
break;
;
int low = i + 1;
int high = n - 1;
int target = -nums.get(i);
while (low < high) {
int sum = nums.get(low) + nums.get(high);
if (sum > target) {
high--;
} else if (sum < target) {
low++;
} else {
List<Integer> temp = Arrays.asList(nums.get(i), nums.get(low), nums.get(high));
result.add(temp);
while (low < high && nums.get(low) == temp.get(1))
low++;
while (low < high && nums.get(high) == nums.get(2))
high--;
}
}
while (i + 1 < n && nums.get(i) == nums.get(i + 1))
i++;
}
return result;
}
public static List<String> splitWords(String s) {
return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> nums = splitWords(scanner.nextLine()).stream().map(Integer::parseInt).collect(Collectors.toList());
scanner.close();
List<List<Integer>> res = tripletsWithSum0(nums);
for (List<Integer> row : res) {
System.out.println(row.stream().map(String::valueOf).collect(Collectors.joining(" ")));
}
}
}