File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 6
6
7
7
public class SearchForARange {
8
8
9
+ /**
10
+ * TestCase
11
+ * [1], 0
12
+ * [1], 1
13
+ */
9
14
public int [] searchRange (int [] nums , int target ) {
15
+ if (nums .length == 0 ) {
16
+ return new int []{-1 , -1 };
17
+ }
18
+ int first = firstHigherEqual (nums , target );
19
+ if (first == nums .length || nums [first ] != target ) {
20
+ return new int [] {
21
+ -1 , -1
22
+ };
23
+ }
24
+ return new int [] {
25
+ first ,
26
+ firstHigherEqual (nums , target + 1 ) - 1
27
+ };
28
+ }
29
+
30
+ private int firstHigherEqual (int [] nums , int target ) {
31
+ int left = 0 , right = nums .length ;
32
+ while (left < right ) {
33
+ int mid = (left + right ) / 2 ;
34
+ if (nums [mid ] < target ) {
35
+ left = mid + 1 ;
36
+ } else {
37
+ right = mid ;
38
+ }
39
+ }
40
+ return left ;
41
+ }
42
+
43
+ public int [] searchRange2 (int [] nums , int target ) {
10
44
if (nums .length == 0 ) {
11
45
return new int []{-1 , -1 };
12
46
}
Original file line number Diff line number Diff line change 1
1
package com .inuker .test ;
2
2
3
+ import com .inuker .solution .SearchForARange ;
3
4
import com .leetcode .library .ListNode ;
4
5
import com .leetcode .library .RandomListNode ;
5
6
import com .leetcode .library .TreeNode ;
27
28
public class main {
28
29
29
30
public static void main (String [] args ) {
31
+ int [] range = new SearchForARange ().searchRange (new int [] {
32
+ 1 , 2 , 3 , 3 , 4
33
+ }, 3 );
34
+ for (int n : range ) {
35
+ System .out .print (n + " " );
36
+ }
30
37
}
31
38
}
You can’t perform that action at this time.
0 commit comments