-
Notifications
You must be signed in to change notification settings - Fork 8
/
TwoSumIIInputArrayIsSorted.java
75 lines (66 loc) · 2 KB
/
TwoSumIIInputArrayIsSorted.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
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
package binarysearch;
// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
// Id : 167
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-06-11
// Topic : Binary Search
// Level : Easy
// Other :
// Tips : If there's a SORTED array, binary-search should come to your mind immediately
// Result : 100.00% 63.39%
import java.util.HashMap;
import java.util.Map;
public class TwoSumIIInputArrayIsSorted {
// 28.02% 63.47%
public int[] twoSumFromOld(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[]{map.get(target - nums[i]) + 1, i + 1}; // one based
}
map.put(nums[i], i);
}
return null;
}
/**
* two pointers
*
* @param nums
* @param target
* @return
*/
// 96.09% 1ms 6.67%
public int[] twoSum1(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] == target)
return new int[]{left + 1, right + 1};
else if (nums[left] + nums[right] < target)
left++;
else
right--;
}
return null;
}
/**
* binary search
*
* @param nums
* @param target
* @return
*/
// 100.00% 63.39%
public int[] twoSum(int[] nums, int target) {
int start = 0, end = nums.length - 1;
while (true) {
int mid = start + (end - start) / 2;
if (nums[start] + nums[end] == target) {
return new int[]{start + 1, end + 1};
} else if (nums[start] + nums[end] < target) {
start = nums[mid] + nums[end] < target ? mid : start + 1;
} else {
end = nums[mid] + nums[start] > target ? mid : end - 1;
}
}
}
}