Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
44 lines (44 loc) · 1.55 KB

File metadata and controls

44 lines (44 loc) · 1.55 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class Solution {
public List<List<Integer>> threeSum(int[] num) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(num);
for (int i = 0; i < num.length-2; i++) {
if (i != 0 && num[i] == num[i-1]) {
continue;
}
int left = i + 1;
int right = num.length - 1;
while (left < right) {
Boolean shrinkRight = false;
Boolean shrinkLeft = false;
int sum = num[i] + num[left] + num[right];
if (sum == 0) {
List<Integer> curr = new ArrayList<Integer>();
curr.add(num[i]);
curr.add(num[left]);
curr.add(num[right]);
result.add(curr);
shrinkLeft = true;
shrinkRight = true;
} else if (sum > 0) {
shrinkRight = true;
} else {
shrinkLeft = true;
}
if (shrinkLeft == true) {
int temp_left = left;
while (left < num.length && num[left] == num[temp_left]) {
left += 1;
}
}
if (shrinkRight == true) {
int temp_right = right;
while (right >= 0 && num[right] == num[temp_right]) {
right -= 1;
}
}
}
}
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.