forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_167.java
79 lines (74 loc) · 2.42 KB
/
_167.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
75
76
77
78
79
package com.fishercoder.solutions;
import java.util.Arrays;
public class _167 {
public static class Solution1 {
/**
* This is an amazing solution!
* Time: O(logn)
*/
public int[] twoSum(int[] numbers, int target) {
int left = 0;
int right = numbers.length - 1;
while (left < right) {
long sum = numbers[left] + numbers[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
return new int[]{left + 1, right + 1};
}
}
return new int[]{-1, -1};
}
}
public static class Solution2 {
/**
* Time: O(nlogn)
*/
public int[] twoSum(int[] numbers, int target) {
for (int i = 0; i < numbers.length - 1; i++) {
int index = exists(Arrays.copyOfRange(numbers, i + 1, numbers.length), target - numbers[i]);
if (index >= 0) {
return new int[]{i + 1, index + 2 + i};
}
}
return null;
}
private int exists(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return nums[left] == target ? left : (right >= 0 && nums[right] == target) ? right : -1;
}
}
public static class Solution3 {
/**
* Time: O(nlogn)
*/
public int[] twoSum(int[] numbers, int target) {
for (int i = 0; i < numbers.length - 1; i++) {
int[] ans = new int[2];
ans[0] = i + 1;
int index = Arrays.binarySearch(Arrays.copyOfRange(numbers, i, numbers.length), target - numbers[i]);
if (index > 0) {
ans[1] = index + 1 + i;
return ans;
} else if (index == 0) {
ans[1] = index + 2 + i;
return ans;
}
}
return null;
}
}
}