-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_threeSum.java
106 lines (95 loc) · 2.48 KB
/
Solution_threeSum.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.bupt.kcrosswind.Leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
public class Solution_threeSum {
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
Arrays.sort(num);
ArrayList<ArrayList<Integer>> reslut = new ArrayList<ArrayList<Integer>>();
if (num.length < 3) {
return reslut;
}
int length = num.length;
for (int sursor = 0; sursor < length - 2; sursor++) {
if (sursor == 0 || num[sursor] > num[sursor - 1]) {
int left = sursor + 1;
int right = num.length - 1;
int target = 0 - num[sursor];
while (left < right) {
if (num[left] + num[right] == target) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(num[sursor]);
temp.add(num[left]);
temp.add(num[right]);
reslut.add(temp);
left++;
right--;
while (left < right && num[left] == num[left + 1]) {
left++;
}
while (left < right && num[right] == num[right - 1]) {
right--;
}
}
while (left < right && num[left] + num[right] < target) {
left++;
}
while (left < right && num[left] + num[right] > target) {
right--;
}
}
}
}
return reslut;
}
public void twoSum(int[] num, int target, int sursor, ArrayList<ArrayList<Integer>> reslut) {
int left = sursor + 1;
int right = num.length - 1;
while (left < right) {
if (num[left] + num[right] == target) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(num[sursor]);
temp.add(left);
temp.add(right);
Collections.sort(temp);
reslut.add(temp);
left++;
right--;
}
while (left < right && num[left] + num[right] < target) {
left++;
}
while (left < right && num[left] + num[right] > target) {
right--;
}
}
}
public static void main(String[] args) {
HashSet<int[]> hs = new HashSet<int[]>();
int[] i = { 1, 2, 3 };
int[] i1 = { 1, 2, 3 };// butong
hs.add(i1);
hs.add(i);
for (int[] flag : hs) {
System.out.println(flag.toString());
}
if (!i.equals(i1)) {
System.out.println("true");
}
HashSet<ArrayList<Integer>> hs1 = new HashSet<ArrayList<Integer>>();
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(1);
al.add(2);
al.add(3);
ArrayList<Integer> al1 = new ArrayList<Integer>();
al1.add(1);
al1.add(2);
al1.add(3);
hs1.add(al);
hs1.add(al1);
for (ArrayList<Integer> flag : hs1) {
System.out.println(flag.toString());
}
}
}