-
Notifications
You must be signed in to change notification settings - Fork 2
/
34.FindRangeInSortedArray.js
64 lines (60 loc) · 1.47 KB
/
34.FindRangeInSortedArray.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
/**
*
✔ Accepted
✔ 88/88 cases passed (84 ms)
✔ Your runtime beats 84.08 % of javascript submissions
✔ Your memory usage beats 35.88 % of javascript submissions (35.1 MB)
*/
var searchRange$1 = function (nums, target) {
var mid = Math.floor(nums.length / 2);
if (nums[0] > target || nums[nums.length - 1] < target) return [-1, -1];
// 判断往那边
var count = 0;
var l = mid;
var r = mid;
if (target > nums[mid]) {
count = 1;
} else if (target < nums[mid]) {
count = -1;
}
while (nums[mid] != target) {
mid += count;
l = mid;
r = mid;
if (mid > nums.length - 1 || mid < 0) {
return [-1, -1];
}
}
while (nums[mid] == nums[mid + 1]) {
mid++;
r = mid;
}
while (nums[mid - 1] == nums[mid]) {
mid--;
l = mid;
}
return [l, r];
};
var searchRange$2 = function (nums, target) {
const res1 = nums.slice(0).reduce((acc, cur, index, arr) => {
if (cur === target) {
acc.push(index);
arr.splice(1);
return acc;
}
return acc;
}, []);
const res2 = nums.slice(0).reduceRight((acc, cur, index, arr) => {
if (cur === target) {
acc.push(index);
arr = [];
return acc;
}
return acc;
}, []);
if (res1.length > 0) {
return res1.concat(res2[0]);
} else {
return [-1, -1];
}
};