Skip to content

Commit 4d8ae3e

Browse files
[N-0] refactor 34
1 parent 592add3 commit 4d8ae3e

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
5-
/**Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
6-
7-
Your algorithm's runtime complexity must be in the order of O(log n).
8-
9-
If the target is not found in the array, return [-1, -1].
3+
/**
4+
* 34. Search for a Range
5+
*
6+
* Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
7+
* Your algorithm's runtime complexity must be in the order of O(log n).
8+
* If the target is not found in the array, return [-1, -1].
109
1110
For example,
1211
Given [5, 7, 7, 8, 8, 10] and target value 8,
1312
return [3, 4].
13+
1414
*/
1515
public class _34 {
1616

17-
public static int[] searchRange(int[] nums, int target) {
18-
int start = 0;
19-
int end = nums.length - 1;
17+
public int[] searchRange(int[] nums, int target) {
2018
int[] range = new int[2];
2119
range[0] = -1;
2220
range[1] = -1;
21+
if (nums == null || nums.length == 0) {
22+
return range;
23+
}
24+
int start = 0;
25+
int end = nums.length - 1;
2326
while (start + 1 < end) {
2427
int mid = start + (end - start) / 2;
2528
if (nums[mid] == target) {
@@ -40,14 +43,14 @@ public static int[] searchRange(int[] nums, int target) {
4043
end = mid;
4144
}
4245
}
46+
4347
if (nums[start] == target) {
4448
range[0] = start;
4549
while (start + 1 < nums.length && nums[start] == nums[start + 1]) {
4650
start++;
4751
}
4852
range[1] = start;
49-
}
50-
if (nums[end] == target) {
53+
} else if (nums[end] == target) {
5154
range[1] = end;
5255
while (end - 1 >= 0 && nums[end] == nums[end - 1]) {
5356
end--;
@@ -56,11 +59,4 @@ public static int[] searchRange(int[] nums, int target) {
5659
}
5760
return range;
5861
}
59-
60-
public static void main(String... strings) {
61-
int[] nums = new int[]{1, 2, 3};
62-
int target = 2;
63-
int[] result = searchRange(nums, target);
64-
CommonUtils.printArray(result);
65-
}
6662
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._34;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _34Test {
10+
private static _34 test;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
test = new _34();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 2, 3};
21+
assertArrayEquals(new int[]{1, 1}, test.searchRange(nums, 2));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{};
27+
assertArrayEquals(new int[]{-1, -1}, test.searchRange(nums, 0));
28+
}
29+
}

0 commit comments

Comments
 (0)