File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number } target
4+ * @return {number[] }
5+ */
6+ var searchRange = function ( nums , target ) {
7+ let startAns = - 1 ;
8+ let endAns = - 1 ;
9+ let start = 0 ;
10+ let end = nums . length - 1 ;
11+ while ( 1 ) {
12+ if ( start > end ) break ;
13+ let m = Math . floor ( ( start + end ) / 2 ) ;
14+ if ( nums [ m ] === target ) {
15+ startAns = m ;
16+ end = m - 1 ;
17+ } else if ( nums [ m ] > target ) {
18+ end = m - 1 ;
19+ } else {
20+ start = m + 1 ;
21+ }
22+ }
23+
24+ let start2 = 0 ;
25+ let end2 = nums . length - 1 ;
26+
27+ while ( 1 ) {
28+ if ( start2 > end2 ) break ;
29+
30+ let m = Math . floor ( ( start2 + end2 ) / 2 ) ;
31+ if ( nums [ m ] === target ) {
32+ endAns = m ;
33+ start2 = m + 1 ;
34+ } else if ( nums [ m ] > target ) {
35+ end2 = m - 1 ;
36+ } else {
37+ start2 = m + 1 ;
38+ }
39+ }
40+ return [ startAns , endAns ] ;
41+ } ;
You can’t perform that action at this time.
0 commit comments