-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
87 lines (78 loc) · 2.37 KB
/
TwoSum.java
File metadata and controls
87 lines (78 loc) · 2.37 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
package easy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
* Example:
* Given nums = [2, 7, 11, 15], target = 9,
* Because nums[0] + nums[1] = 2 + 7 = 9,
* return [0, 1].
*
*
* @author chenyiAlone
*/
public class TwoSum {
/**
* 1. for 2 => O(n^2)
*/
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i; j < nums.length; j++) {
int answer = nums[i] + nums[j];
if ((answer == target) && i != j)
return new int[]{i, j};
}
}
return null;
}
/**
* 3. hash
*/
public int[] twoSumHash(int[] nums, int target) {
if (nums.length < 1)
return new int[]{};
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int n = nums[i];
if (map.containsKey(n)) {
return new int[]{map.get(n), i};
}
map.put(target - n, i);
}
return new int[]{};
}
static class Pair implements Comparable<Pair> {
public int left = 0;
public int right = 0;
Pair(int left, int right) {
this.left = left;
this.right = right;
}
@Override
public int compareTo(Pair target) {
return this.left - target.left;
}
}
/**
* 3. sort+ 双指针
*
*/
public int[] twoSumDoublePointer(int[] nums, int target) {
if (nums == null || nums.length < 1) return new int[]{};
Pair[] pairs = new Pair[nums.length];
for (int i = 0; i < nums.length; i++) {
pairs[i] = new Pair(nums[i], i);
}
Arrays.sort(pairs);
int l = 0, r = nums.length - 1;
while (l < r) {
int sum = pairs[l].left + pairs[r].left;
if (sum < target) l++;
else if (sum > target) r--;
else return new int[]{pairs[l].right, pairs[r].right};
}
return new int[]{};
}
}