-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-first-and-last-position-of-element-in-sorted-array.js
62 lines (58 loc) · 1.49 KB
/
find-first-and-last-position-of-element-in-sorted-array.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
/**
* 在排序数组中查找元素的第一个和最后一个位置
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var searchRange = function (nums, target) {
// 2. 二分查找 O(log n)
const binarySearch = (nums, target, isLeftBoundary = true) => {
let left = 0;
let right = nums.length - 1;
let middle,
res = -1;
while (left <= right) {
middle = Math.floor((left + right) / 2);
if (target < nums[middle]) {
right = middle - 1;
} else if (target > nums[middle]) {
left = middle + 1;
} else {
res = middle;
if (isLeftBoundary) {
right = middle - 1;
} else {
left = middle + 1;
}
}
}
return res;
};
let result = [-1, -1];
// 找左边界,由右侧逼近
result[0] = binarySearch(nums, target, true);
// 找右边界,由左侧逼近
result[1] = binarySearch(nums, target, false);
return result;
// 1. 双指针 O(n)
// let left = 0;
// let right = nums.length - 1;
// let result = [-1, -1];
// while (left <= right) {
// if (nums[left] !== target) {
// left++;
// } else if (result[0] === -1) {
// result[0] = left;
// }
// if (nums[right] !== target) {
// right--;
// } else if (result[1] === -1) {
// result[1] = right;
// }
// // 如果全部找到,则直接退出
// if (result[0] !== -1 && result[1] !== -1) {
// break;
// }
// }
// return result;
};