forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindTripletsWithGivenSum.java
46 lines (40 loc) · 1.4 KB
/
FindTripletsWithGivenSum.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
/*https://leetcode.com/problems/3sum/*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if (nums.length < 3) return list;
Arrays.sort(nums);
for (int i = 0; i < nums.length-2; ++i)
{
//do not consider same elements
if (i > 0 && nums[i] == nums[i-1])
continue;
//set two pointers after the fixed pointer
int start = i+1;
int end = nums.length-1;
while (start < end)
{
//get the sum
int sum = nums[i]+nums[start]+nums[end];
//if sum is 0
if (sum == 0)
{
//add the numbers
list.add(Arrays.asList(nums[i],nums[start],nums[end]));
//do not consider same elements
int currStart = nums[start];
int currEnd = nums[end];
while (start < end && nums[start] == currStart) ++start;
while (start < end && nums[end] == currEnd) --end;
}
//if sum is positive, move left
else if (sum > 0)
--end;
//else move right
else
++start;
}
}
return list;
}
}