-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSolution.java
225 lines (202 loc) · 7.04 KB
/
Solution.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.example.lib;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
public class Solution {
/**
* 暴力解法
*
* @param arr 数组
* @param target 目标
* @return
*/
public static int[] twoSum(int[] arr, int target) {
if (arr.length < 2) {
return null;
}
for (int i = 0; i < arr.length - 1; i++) {
//每个人
for (int j = i + 1; j < arr.length; j++) {
// 都去问其他的人
if (arr[i] + arr[j] == target) {
return new int[]{arr[i], arr[j]};
}
}
}
return null;
}
/**
* 优化
*
* @param arr
* @param target
* @return
*/
public static int[] twosum2(int[] arr, int target) {
if (arr.length < 2) {
return null;
}
HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
// 每个人登记自己想要配对的人,让主持人记住
for (int i = 0; i < arr.length; i++) {
integerIntegerHashMap.put(target - arr[i], i);
}
for (int j = 0; j < arr.length; j++) {
// 每个人再次报数的时候,主持人看一下名单里有没有他
Integer integer = integerIntegerHashMap.get(arr[j]);
if (integer != null && integer != j) {
return new int[]{arr[integer], arr[j]};
}
}
return null;
}
/**
* 在优化
*
* @param arr
* @param target
* @return
*/
public static int[] twosum3(int[] arr, int target) {
if (arr.length < 2) {
return null;
}
HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
for (int j = 0; j < arr.length; j++) {
// 每个人报出自己想要配对的人
Integer integer = integerIntegerHashMap.get(arr[j]);
if (integer != null) {
// 如果有人被登记过
return new int[]{arr[j], integer};
} else {
integerIntegerHashMap.put(target - arr[j], arr[j]);
}
}
return null;
}
public static ArrayList<ResultBean> threeSum(int[] arr) {
HashSet<ResultBean> ints = new HashSet<>();
if (arr.length < 3) {
return null;
}
// 每个人
for (int i = 0; i < arr.length - 2; i++) {
// 依次拉上其他每个人
for (int j = i + 1; j < arr.length - 1; j++) {
// 去问剩下的每个人
for (int k = j + 1; k < arr.length; k++) {
// 我们是不是可以一起组队
if (arr[i] + arr[j] + arr[k] == 0) {
ResultBean resultBean = new ResultBean(new int[]{arr[i], arr[j], arr[k]});
ints.add(resultBean);
}
}
}
}
return new ArrayList<>(ints);
}
public static ArrayList<ResultBean> threeSum1(int[] nums) {
ArrayList<ResultBean> result = new ArrayList<ResultBean>();
if (nums.length < 3) {
return result;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue; //选定nums[i]为第一个数,并去重
}
int left = i + 1;
int right = nums.length - 1;
while (right > left) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
ResultBean resultBean = new ResultBean(new int[]{nums[i], nums[left], nums[right]});
result.add(resultBean);
while (left < right && nums[left] == nums[left + 1]) {
left++; //去重
}
while (left + 1 < right && nums[right] == nums[right - 1]) {
right--;
}
}
if (sum <= 0) {
left++;
} else {
right--;
}
}
}
return result;
}
public static ArrayList<ResultBean> threeSum2(int[] nums) {
ArrayList<ResultBean> result = new ArrayList<ResultBean>();
if (nums.length < 3) {
return result;
}
Arrays.sort(nums);
// 优化1: 整个数组同符号,则无解
if (nums[0] * nums[nums.length - 1] > 0) {
return result;
}
for (int i = 0; i < nums.length; i++) {
// 优化2: 最左值为正数则一定无解
if (i > 0 && nums[i] == nums[i - 1] || nums[i] > 0) {
continue; //选定nums[i]为第一个数,并去重
}
int left = i + 1;
int right = nums.length - 1;
while (right > left) {
if (nums[i] * nums[right] > 0) {
break;// 两人选相遇,或者三人同符号,则退出
}
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
ResultBean resultBean = new ResultBean(new int[]{nums[i], nums[left], nums[right]});
result.add(resultBean);
while (left < right && nums[left] == nums[left + 1]) {
left++; //去重
}
while (left + 1 < right && nums[right] == nums[right - 1]) {
right--;
}
}
if (sum <= 0) {
left++;
} else {
right--;
}
}
}
return result;
}
public static HashSet<ResultBean> threeSumHash(int[] arrs) {
HashSet<ResultBean> resultBeans = new HashSet<>();
for (int i = 0; i < arrs.length; i++) {
int tager = arrs[i];
HashSet<ResultBean> twoSumHashResult = twoSumHash(-tager, arrs, i);
resultBeans.addAll(twoSumHashResult);
}
return resultBeans;
}
public static HashSet<ResultBean> twoSumHash(int targer, int[] arr, int skip) {
HashSet<ResultBean> resultBeans = new HashSet<>();
HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
for (int j = 0; j < arr.length; j++) {
if (skip == j) {
continue;
}
// 每个人报出自己想要配对的人
Integer integer = integerIntegerHashMap.get(arr[j]);
if (integer != null) {
// 如果有人被登记过
ResultBean resultBean = new ResultBean(new int[]{arr[j], integer, -targer});
resultBeans.add(resultBean);
} else {
integerIntegerHashMap.put(targer - arr[j], arr[j]);
}
}
return resultBeans;
}
}