forked from webVueBlog/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
在排序数组中查找数字.js
72 lines (68 loc) · 1.54 KB
/
在排序数组中查找数字.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
69
70
71
72
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
剑指 Offer 53 - I. 在排序数组中查找数字 I
二分
[1,2,3,5,7]
[5,7,7,8,8,10] target = 9 // 6 // 8
l r(m) r
m (l=m+1)
搜索左边界 // l 是否等于 target
v(m) < target l = m + 1;
v(m) > target r = m;
v(m) === target r = m;
// 判断 l 是否等于 target
v(m) < target l = m + 1;
v(m) > target r = m;
v(m) === target l = m + 1;
[l, r) => while(l < r)
[l, r] => while(l <= r)
剑指 Offer 53 - I. 在排序数组中查找数字 I
*/
var search = function(nums, target) {
const l = findLeft(nums, target);
const r = findRight(nums, target);
if(l === -1) {
return 0;
}
return r - l + 1; //出现次数
};
function findLeft(nums, target) {
// 值 / -1
let l = 0, r = nums.length;
while(l < r){
const m = l + ((r-l)>>1);
const mid = nums[m];
if(mid === target) {
r = m;
} else if(mid > target) {
r = m;
} else if(mid < target) {
l = m+1;
}
}
if(nums[l] === target) {
return l;
}
return -1;
}
function findRight(nums, target) {
// 值 / -1
let l = 0, r = nums.length;
while(l < r){
const m = l + ((r-l)>>1);
const mid = nums[m];
if(mid === target) {
l = m + 1;
} else if(mid > target) {
r = m;
} else if(mid < target) {
l = m+1;
}
}
if(nums[l-1] === target) {
return l-1;
}
return -1;
}