-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo_Sum_II.java
More file actions
98 lines (81 loc) · 2.75 KB
/
Two_Sum_II.java
File metadata and controls
98 lines (81 loc) · 2.75 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
class Solution {
public int[] twoSum(int[] num, int target) {
int[] indice = new int[2];
if (num == null || num.length < 2) return indice;
int left = 0, right = num.length - 1;
while (left < right) {
int v = num[left] + num[right];
if (v == target) {
indice[0] = left + 1;
indice[1] = right + 1;
break;
} else if (v > target) {
right --;
} else {
left ++;
}
}
return indice;
}
}
// Another solution
class Solution {
public int[] twoSum(int[] numbers, int target) {
int start = 0, end = numbers.length - 1;
while(start < end){
if(numbers[start] + numbers[end] == target) break;
if(numbers[start] + numbers[end] < target) start++;
else end--;
}
return new int[]{start + 1, end + 1};
}
}
// Complex solution
class Solution {
public int[] twoSum(int[] numbers, int target) {
if (numbers == null || numbers.length == 0) {
return new int[2];
}
int start = 0;
int end = numbers.length - 1;
while (start < end) {
if (numbers[start] + numbers[end] == target) {
return new int[]{start + 1, end + 1};
} else if (numbers[start] + numbers[end] > target) {
// move end forward to the last value that numbers[end] <= target - numbers[start]
end = largestSmallerOrLastEqual(numbers, start, end, target - numbers[start]);
} else {
// move start backword to the first value that numbers[start] >= target - numbers[end]
start = smallestLargerOrFirstEqual(numbers, start, end, target - numbers[end]);
}
}
return new int[2];
}
private int largestSmallerOrLastEqual(int[] numbers, int start, int end, int target) {
int left = start;
int right = end;
while (left <= right) {
int mid = left + (right - left) / 2;
if (numbers[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
private int smallestLargerOrFirstEqual(int[] numbers, int start, int end, int target) {
int left = start;
int right = end;
while (left <= right) {
int mid = left + (right - left) / 2;
if (numbers[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
}
// It is the best case O(logN) solution but worst case O(NlogN). So two pointer solution is best for this problem.