-
Notifications
You must be signed in to change notification settings - Fork 0
/
Three_sum.java
40 lines (36 loc) · 1.12 KB
/
Three_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
package com.company.LeetCode;
import java.util.*;
// https://leetcode.com/problems/3sum/
public class Three_sum {
public static void main(String[] args) {
int[] arr = {-1,0,1,2,-1,-4};
System.out.println(threeSum(arr));
}
static List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> list = new HashSet<>();
if (nums.length < 3){
return new ArrayList<>(list);
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int target = -nums[i];
int start = i + 1;
int end = nums.length - 1;
while (start < end){
int sum = nums[start] + nums[end];
if(sum < target){
start++;
}
else if(sum > target){
end--;
}
else{
list.add(List.of(nums[i], nums[start], nums[end]));
start++;
end--;
}
}
}
return new ArrayList<>(list);
}
}