Skip to content

Commit 0460edc

Browse files
refactor 167
1 parent 8010a74 commit 0460edc

File tree

2 files changed

+95
-20
lines changed

2 files changed

+95
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 167. Two Sum II - Input array is sorted
5-
6-
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
7-
8-
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
9-
10-
Note:
11-
12-
Your returned answers (both index1 and index2) are not zero-based.
13-
You may assume that each input would have exactly one solution and you may not use the same element twice.
14-
15-
Example:
16-
17-
Input: numbers = [2,7,11,15], target = 9
18-
Output: [1,2]
19-
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
20-
21-
*/
3+
import java.util.Arrays;
224

235
public class _167 {
246
public static class Solution1 {
7+
/**
8+
* This is an amazing solution!
9+
* Time: O(logn)
10+
*/
2511
public int[] twoSum(int[] numbers, int target) {
2612
int left = 0;
2713
int right = numbers.length - 1;
@@ -38,4 +24,56 @@ public int[] twoSum(int[] numbers, int target) {
3824
return new int[]{-1, -1};
3925
}
4026
}
27+
28+
public static class Solution2 {
29+
/**
30+
* Time: O(nlogn)
31+
*/
32+
public int[] twoSum(int[] numbers, int target) {
33+
for (int i = 0; i < numbers.length - 1; i++) {
34+
int index = exists(Arrays.copyOfRange(numbers, i + 1, numbers.length), target - numbers[i]);
35+
if (index >= 0) {
36+
return new int[]{i + 1, index + 2 + i};
37+
}
38+
}
39+
return null;
40+
}
41+
42+
private int exists(int[] nums, int target) {
43+
int left = 0;
44+
int right = nums.length - 1;
45+
while (left < right) {
46+
int mid = left + (right - left) / 2;
47+
if (nums[mid] == target) {
48+
return mid;
49+
} else if (nums[mid] < target) {
50+
left = mid + 1;
51+
} else {
52+
right = mid - 1;
53+
}
54+
}
55+
return nums[left] == target ? left : (right >= 0 && nums[right] == target) ? right : -1;
56+
}
57+
}
58+
59+
public static class Solution3 {
60+
/**
61+
* Time: O(nlogn)
62+
*/
63+
public int[] twoSum(int[] numbers, int target) {
64+
for (int i = 0; i < numbers.length - 1; i++) {
65+
int[] ans = new int[2];
66+
ans[0] = i + 1;
67+
int index = Arrays.binarySearch(Arrays.copyOfRange(numbers, i, numbers.length), target - numbers[i]);
68+
if (index > 0) {
69+
ans[1] = index + 1 + i;
70+
return ans;
71+
} else if (index == 0) {
72+
ans[1] = index + 2 + i;
73+
return ans;
74+
}
75+
}
76+
return null;
77+
}
78+
}
4179
}

src/test/java/com/fishercoder/_167Test.java

+38-1
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,55 @@
88

99
public class _167Test {
1010
private static _167.Solution1 solution1;
11+
private static _167.Solution2 solution2;
12+
private static _167.Solution3 solution3;
1113
private static int[] numbers;
1214
private static int[] expected;
15+
private int target;
1316

1417
@BeforeClass
1518
public static void setup() {
1619
solution1 = new _167.Solution1();
20+
solution2 = new _167.Solution2();
21+
solution3 = new _167.Solution3();
1722
}
1823

1924
@Test
2025
public void test1() {
2126
numbers = new int[]{-3, 3, 4, 90};
2227
expected = new int[]{1, 2};
23-
assertArrayEquals(expected, solution1.twoSum(numbers, 0));
28+
target = 0;
29+
assertArrayEquals(expected, solution1.twoSum(numbers, target));
30+
assertArrayEquals(expected, solution2.twoSum(numbers, target));
31+
assertArrayEquals(expected, solution3.twoSum(numbers, target));
32+
}
33+
34+
@Test
35+
public void test2() {
36+
expected = new int[]{2, 3};
37+
target = 100;
38+
assertArrayEquals(expected, solution1.twoSum(new int[]{5, 25, 75}, target));
39+
assertArrayEquals(expected, solution2.twoSum(new int[]{5, 25, 75}, target));
40+
assertArrayEquals(expected, solution3.twoSum(new int[]{5, 25, 75}, target));
41+
}
42+
43+
@Test
44+
public void test3() {
45+
numbers = new int[]{1, 2, 3, 4, 4, 9, 56, 90};
46+
expected = new int[]{4, 5};
47+
target = 8;
48+
assertArrayEquals(expected, solution1.twoSum(numbers, target));
49+
assertArrayEquals(expected, solution2.twoSum(numbers, target));
50+
assertArrayEquals(expected, solution3.twoSum(numbers, target));
51+
}
52+
53+
@Test
54+
public void test4() {
55+
numbers = new int[]{2, 3, 4};
56+
expected = new int[]{1, 3};
57+
target = 6;
58+
assertArrayEquals(expected, solution1.twoSum(numbers, target));
59+
assertArrayEquals(expected, solution2.twoSum(numbers, target));
60+
assertArrayEquals(expected, solution3.twoSum(numbers, target));
2461
}
2562
}

0 commit comments

Comments
 (0)