-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTwoSumDemo.java
More file actions
78 lines (65 loc) · 2.26 KB
/
TwoSumDemo.java
File metadata and controls
78 lines (65 loc) · 2.26 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package TwoSum;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class TwoSumDemo {
public static void main(String[] args) {
int[] table = {1, 2, 10, 15, 7, 3, 20, 100, 25, 96, 4, 5, 6, 8, 9, 11};
int target = 13;
int[] result = findTwoSumMap(table, target);
if (result != null)
System.out.println(target + " = " + table[result[0]] + " + " + table[result[1]]);
}
// solution #1: using brute force (complexity: O(n²))
public static int[] findTwoSumBruteForce(int[] array, int n) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (i == j)
continue;
if (array[i] + array[j] == n) {
return new int[]{i, j};
}
}
}
return null;
}
// solution #2: using map data structure (complexity: O(n))
public static int[] findTwoSumMap(int[] array, int target) {
Map<Integer, Integer> visitedNumbers = new HashMap<>();
for (int i = 0; i < array.length; i++) {
int neededNum = target - array[i];
if(visitedNumbers.containsKey(neededNum)) {
return new int[]{i, visitedNumbers.get(neededNum)};
}
visitedNumbers.put(array[i], i);
}
return null;
}
// solution #3: using set data structure (complexity: O(n))
public static int[] findTwoSumSet(int[] array, int n) {
Set<Integer> numbers = new HashSet<>();
for (int i = 0; i < array.length; i++) {
numbers.add(array[i]);
}
int firstIndice = -1, target = -1;
boolean isFound = false;
for (int i = 0; i < array.length; i++) {
target = n - array[i];
if (target == array[i] || target < 0)
continue;
if (numbers.contains(target)) {
isFound = true;
firstIndice = i;
break;
}
}
if (isFound) {
for (int i = 0; i < array.length; i++) {
if (target == array[i])
return new int[]{firstIndice, i};
}
}
return null;
}
}