Skip to content

Commit 1a41ce7

Browse files
committed
Added test case in search for a range
1 parent b5cf1b5 commit 1a41ce7

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Medium/SearchForARange.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
*/
1515
class SearchForARange {
1616
public static void main(String[] args) {
17-
17+
SearchForARange s = new SearchForARange();
18+
int[] A = { 1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
19+
int[] range = s.searchRange(A, 3);
20+
System.out.println(range[0] + " ~ " + range[1]);
1821
}
1922

2023
/**
21-
* Do binary search on left and right
24+
* Suppose we have a binary search helper method
25+
* With array, start index, end index, and target as arguments
26+
* We can first search for the target in the whole array
27+
* If found, then search for its starting position
28+
* Then search for its ending position
29+
* Update range with search result and return
2230
*/
2331
public int[] searchRange(int[] A, int target) {
2432
int[] range = {-1, -1};
@@ -44,4 +52,4 @@ private int binarySearch(int[] A, int left, int right, int target) {
4452
}
4553
return -1;
4654
}
47-
}
55+
}

0 commit comments

Comments
 (0)