Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a4de6bd

Browse files
refactor 167
1 parent d1fab0e commit a4de6bd

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
/**
64
* 167. Two Sum II - Input array is sorted
75
*
8-
* 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.
9-
10-
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. Please note that your returned answers (both index1 and index2) are not zero-based.
11-
12-
You may assume that each input would have exactly one solution.
6+
* Given an array of integers that is already sorted in ascending order,
7+
* find two numbers such that they add up to a specific target number.
8+
* The function twoSum should return indices of the two numbers such that they add up to the target,
9+
* where index1 must be less than index2.
10+
* Please note that your returned answers (both index1 and index2) are not zero-based.
11+
* You may assume that each input would have exactly one solution.
1312
1413
Input: numbers={2, 7, 11, 15}, target=9
1514
Output: index1=1, index2=2
16-
1715
*/
16+
1817
public class _167 {
19-
public static int[] twoSum(int[] numbers, int target) {
20-
int left = 0, right = numbers.length-1;
18+
19+
public int[] twoSum(int[] numbers, int target) {
20+
int left = 0, right = numbers.length - 1;
2121
int[] result = new int[2];
22-
while(numbers[right] > target) right--;
23-
if(right < numbers.length-1) right++;
24-
while(left <= right){
22+
while (numbers[right] > target) {
23+
right--;
24+
}
25+
if (right < numbers.length - 1) {
26+
right++;
27+
}
28+
while (left <= right) {
2529
int sum = numbers[left] + numbers[right];
26-
if(sum > target) right--;
27-
else if(sum < target) left++;
28-
else if(sum == target){
29-
result[0] = left+1;
30-
result[1] = right+1;
30+
if (sum > target) {
31+
right--;
32+
} else if (sum < target) {
33+
left++;
34+
} else if (sum == target) {
35+
result[0] = left + 1;
36+
result[1] = right + 1;
3137
break;
3238
}
3339
}
3440
return result;
3541
}
36-
37-
public static void main(String...strings){
38-
int[] nums = new int[]{-3,3,4,90};
39-
int k = 0;
40-
int[] result = twoSum(nums, k);
41-
CommonUtils.printArray(result);
42-
}
42+
4343
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._167;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _167Test {
10+
private static _167 test;
11+
private static int[] numbers;
12+
private static int[] expected;
13+
14+
@BeforeClass
15+
public static void setup(){
16+
test = new _167();
17+
}
18+
19+
@Test
20+
public void test1(){
21+
numbers = new int[]{-3, 3, 4, 90};
22+
expected = new int[]{1,2};
23+
assertArrayEquals(expected, test.twoSum(numbers, 0));
24+
}
25+
}

0 commit comments

Comments
 (0)