forked from Gerkins/arithmeticSum
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTwoSumPractest01.java
34 lines (29 loc) · 1.01 KB
/
TwoSumPractest01.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
import java.util.Hashtable;
/**
* Created by lrkin on 2016/10/28.
*/
public class TwoSumPractest01 {
public void twoSum(int[] array, int key) {
Hashtable<Integer, Integer> hashTable
= new Hashtable<Integer, Integer>();
for (int i = 0; i < array.length; i++) {
hashTable.put(array[i], i);
}
for (int i = 0; i < array.length; i++) {
int diff = key - array[i];
if (hashTable.get(diff) != null) {
int[] result = {array[i], array[hashTable.get(diff)] , i , hashTable.get(diff)};
for (int j = 0; j < result.length; j++) {
System.out.print(result[j] + "-");
// break;
}
System.out.println("");
}
}
}
public static void main(String[] args) {
TwoSumPractest01 twoSumPractest01 = new TwoSumPractest01();
int[] array = {6, 4,4,3, 3, 10,10, 8, 12};
twoSumPractest01.twoSum(array, 13);
}
}