-
Notifications
You must be signed in to change notification settings - Fork 33
/
034-Search-for-a-Range.js
43 lines (41 loc) · 1.11 KB
/
034-Search-for-a-Range.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
*
* https://leetcode.com/problems/search-for-a-range/description/
* Difficulty:Medium
*
* Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
* Your algorithm's runtime complexity must be in the order of O(log n).
* If the target is not found in the array, return [-1, -1].
* For example,
* Given [5, 7, 7, 8, 8, 10] and target value 8,
* return [3, 4].
*
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function (nums, target) {
var i = 0;
var j = nums.length - 1;
var ret = [-1, -1];
while (i < j) {
var mid = Math.floor((i + j) / 2);
// console.log(i, mid, j);
if (nums[mid] < target) i = mid + 1;
else j = mid;
}
if (nums[i] !== target) return ret;
ret[0] = i;
j = nums.length - 1;
while (i < j) {
mid = Math.ceil((i + j) / 2);
// console.log(i, mid, j);
if (nums[mid] > target) j = mid - 1;
else i = mid;
}
ret[1] = j;
return ret;
};
console.log(searchRange([5, 7, 7, 8, 8, 10], 8));