-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
68 lines (59 loc) · 1.83 KB
/
res.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 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].
/**
* res.js
* @authors Joe Jiang ([email protected])
* @date 2017-02-26 22:56:22
* @version $Id$
*
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
let searchRange = function(nums, target) {
let arrlen = nums.length;
if (!arrlen) {
return [-1, -1];
}
let leftIndex = binarySearch(nums, 0, arrlen-1, target, 'l'), rightIndex;
if (leftIndex === -1) {
return [-1, -1];
} else {
rightIndex = binarySearch(nums, leftIndex, arrlen-1, target, 'r');
}
return [leftIndex, rightIndex];
};
let binarySearch = function(nums, begin, end, target, type) {
let arrlen = nums.length;
while (begin <= end) {
let mid = begin + Number.parseInt( (end-begin)/2 ),
midVal = nums[mid];
if (midVal === target) {
switch (type) {
case 'l':
if (mid === 0 || nums[mid-1] !== target) {
return mid;
} else {
end = mid-1;
}
break;
default:
if (mid === arrlen-1 || nums[mid+1] !== target) {
return mid;
} else {
begin = mid+1;
}
break;
}
} else if (midVal > target) {
end = mid-1;
} else if (midVal < target) {
begin = mid+1;
}
}
return -1;
};