-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
60 lines (49 loc) · 1.62 KB
/
TwoSum.java
File metadata and controls
60 lines (49 loc) · 1.62 KB
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
package nSum;
import java.util.ArrayList;
import java.util.Arrays;
/**
* 1. 输入的数据为整形,顺序随机
* 2.
*/
public class TwoSum {
public ArrayList<ArrayList<Integer>> twoSum(int[] nums, int target) {
// 对数组进行排序,默认从小到大排序
// 如果需要降序,那么添加Collections.reverseOrder()属性
Arrays.sort(nums);
int low=0, high=nums.length-1;
// System.out.println(nums.length); // 4
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
while(low<high) {
if(nums[low]+nums[high]<target) {
low++;
}else if(nums[low]+nums[high]>target) {
high--;
// 相等时存储
}else {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(nums[low]);
temp.add(nums[high]);
list.add(temp);
int origin = nums[low];
low++;
while(origin == nums[low] && low<high) {
low++;
}
}
}
return list;
}
public static void main(String[] args) {
TwoSum solution = new TwoSum();
int[] nums = new int[] {1,3,5,2};
ArrayList<ArrayList<Integer>> res = solution.twoSum(nums, 5);
for(int i=0;i<res.size();i++) {
System.out.print("[");
for(int j=0;j<res.get(i).size();j++) {
System.out.print(res.get(i).get(j)+" ");
}
System.out.print("]\t");
}
System.out.println();
}
}