forked from zzxboy1/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search_for_a_Range.js
30 lines (30 loc) · 922 Bytes
/
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
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var search = function(left, right, result, nums, target) {
if (left > right) {
return;
}
var pos = Math.floor((right + left) / 2);
if (nums[pos] === target) {
if (result[0] === -1 && result[1] === -1) {
result[0] = result[1] = pos;
} else {
result[0] = pos < result[0] ? pos : result[0];
result[1] = pos > result[1] ? pos : result[1];
}
search(left, pos - 1, result, nums, target);
search(pos + 1, right, result, nums, target);
} else if (nums[pos] > target) {
search(left, pos - 1, result, nums, target);
} else {
search(pos + 1, right, result, nums, target);
}
};
var searchRange = function(nums, target) {
var result = [-1, -1];
search(0, nums.length - 1, result, nums, target);
return result;
};