forked from Gerkins/arithmeticSum
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTwoSumPractest02.java
More file actions
56 lines (50 loc) · 1.4 KB
/
TwoSumPractest02.java
File metadata and controls
56 lines (50 loc) · 1.4 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* Created by lrkin on 2016/10/28.
* <p>
* 题目:
* <p>
* {-2,0,4,4,3,3,9,8,12}
* <p>
* key = 7
* <p>
* 输出:
* <p>
* (-2 , 9)
* (3,4)
*/
public class TwoSumPractest02 {
public ArrayList<int[]> twoSum(int[] array, int key) {
ArrayList<int[]> result = new ArrayList<int[]>();
for (int i = 0; i < array.length; i++) {
if ((i + 1) <= array.length - 1 && array[i] == array[i + 1]) {
continue;
}
for (int k = i; k < array.length; k++) {
if ((k + 1) <= array.length - 1 && array[k] == array[k + 1]) {
continue;
}
if ((array[i] + array[k]) == key) {
int[] arr = {array[i], array[k]};
result.add(arr);
}
}
}
return result;
}
public static void main(String[] args) {
TwoSumPractest02 twoSumPractest02 = new TwoSumPractest02();
int[] array = {-2, 0, 4, 4, 3, 3, 9, 8, 12};
int key = 7;
ArrayList<int[]> result = twoSumPractest02.twoSum(array, key);
for (int[] item : result) {
for (int i = 0; i < item.length; i++) {
System.out.print(item[i] + "-");
}
System.out.println("");
}
}
}