-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
132 lines (122 loc) · 3.03 KB
/
TwoSum.java
File metadata and controls
132 lines (122 loc) · 3.03 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
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
package codingInterview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
class Indice {
int index1;
int index2;
Indice() {
}
Indice(int index1, int index2) {
this.index1 = index1;
this.index2 = index2;
}
}
public class TwoSum {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = { 2, 7, 4, 11, 4, 8, 13, 1, 7, 0};
int target = 8;
// ArrayList<Indice> result = myTwoSumForSorted(nums, target);
// ArrayList<Indice> result = myTwoSum(nums, target);
// ArrayList<Indice> result = mapTwoSum(nums, target);
ArrayList<Indice> result = TwoSumForSorted(nums, target);
for (Indice indice : result) {
System.out.print("[" + indice.index1 + ", " + indice.index2 + "] ");
}
System.out.println();
}
private static ArrayList<Indice> TwoSumForSorted(int[] nums, int target) {
// TODO Auto-generated method stub
ArrayList<Indice> result = new ArrayList<Indice>();
int i = 0;
int j = nums.length - 1;
Arrays.sort(nums);
while (i < j) {
int tmp = nums[i] + nums[j];
if (tmp < target) {
i++;
} else if (tmp > target) {
j--;
} else {
Indice indice = new Indice(i + 1, j + 1);
result.add(indice);
i++;
j--;
}
}
return result;
}
/**
* use hashmap, O(n)
* @param nums
* @param target
* @return
*/
private static ArrayList<Indice> mapTwoSum(int[] nums, int target) {
// TODO Auto-generated method stub
HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
ArrayList<Indice> result = new ArrayList<Indice>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
Indice indice = new Indice(i + 1, map.get(nums[i]) + 1);
result.add(indice);
} else {
map.put(target - nums[i], i);
}
}
return result;
}
/**
* naive solution
* @param nums
* @param target
* @return
*/
private static ArrayList<Indice> myTwoSum(int[] nums, int target) {
// TODO Auto-generated method stub
ArrayList<Indice> result = new ArrayList<Indice>();
ArrayList<Integer> visited = new ArrayList<Integer>();
int len = nums.length;
for (int i = 0; i < len; i++) {
if (visited.contains(i)) {
continue;
}
for (int j = i + 1; j < len; j++) {
if (nums[i] + nums[j] == target) {
Indice indice = new Indice(Math.min(i + 1, j + 1),
Math.max(i + 1, j + 1));
result.add(indice);
visited.add(j);
}
}
}
return result;
}
/**
* for sorted array, so limited
*
* @param nums
* @param target
* @return
*/
private static ArrayList<Indice> myTwoSumForSorted(int[] nums, int target) {
// TODO Auto-generated method stub
ArrayList<Indice> result = new ArrayList<Indice>();
Arrays.sort(nums);
int len = nums.length;
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (nums[i] + nums[j] == target) {
Indice indice = new Indice(Math.min(i + 1, j + 1),
Math.max(i + 1, j + 1));
result.add(indice);
}
if (nums[i] + nums[j] > target) {
continue;
}
}
}
return result;
}
}