File tree Expand file tree Collapse file tree
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66
77public class SearchForARange {
88
9+ /**
10+ * TestCase
11+ * [1], 0
12+ * [1], 1
13+ */
914 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 ) {
1044 if (nums .length == 0 ) {
1145 return new int []{-1 , -1 };
1246 }
Original file line number Diff line number Diff line change 11package com .inuker .test ;
22
3+ import com .inuker .solution .SearchForARange ;
34import com .leetcode .library .ListNode ;
45import com .leetcode .library .RandomListNode ;
56import com .leetcode .library .TreeNode ;
2728public class main {
2829
2930 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+ }
3037 }
3138}
You can’t perform that action at this time.
0 commit comments