-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_two_points_n2.java
58 lines (50 loc) · 1.59 KB
/
AC_two_points_n2.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_two_points_n2.java
* Create Date: 2015-03-03 16:17:09
* Descripton:
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Solution {
public List<List<Integer>> threeSum(int[] num) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
int len = num.length, tar = 0;
if (len <= 2)
return ret;
Arrays.sort(num);
for (int i = 0; i <= len - 3; i++) {
// first number : num[i]
int j = i + 1; // second number
int k = len - 1; // third number
while (j < k) {
if (num[i] + num[j] + num[k] < tar) {
++j;
} else if (num[i] + num[j] + num[k] > tar) {
--k;
} else {
ret.add(Arrays.asList(num[i], num[j], num[k]));
++j;
--k;
// folowing 3 while can avoid the duplications
while (j < k && num[j] == num[j - 1])
++j;
while (j < k && num[k] == num[k + 1])
--k;
}
}
while (i <= len - 3 && num[i] == num[i + 1])
++i;
}
return ret;
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 3, 1};
System.out.println("no case");
}
}