统计一个数字在排序数组中出现的次数。
示例 1:
输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
示例 2:
输入: nums = [5,7,7,8,8,10], target = 6
输出: 0
限制:
0 <= 数组长度 <= 50000
两遍二分,分别查找出左边界和右边界。
class Solution:
def search(self, nums: List[int], target: int) -> int:
if len(nums) == 0:
return 0
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) >> 1
if nums[mid] >= target:
right = mid
else:
left = mid + 1
if nums[left] != target:
return 0
l, right = left, len(nums) - 1
while left < right:
mid = (left + right + 1) >> 1
if nums[mid] <= target:
left = mid
else:
right = mid - 1
return left - l + 1
class Solution {
public int search(int[] nums, int target) {
if (nums.length == 0) {
return 0;
}
// find first position
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = (left + right) >>> 1;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
if (nums[left] != target) {
return 0;
}
int l = left;
// find last position
right = nums.length - 1;
while (left < right) {
int mid = (left + right + 1) >>> 1;
if (nums[mid] <= target) {
left = mid;
} else {
right = mid - 1;
}
}
return left - l + 1;
}
}
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
int left = 0, right = n;
int first, last;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
if (left == n || nums[left] != target) {
return 0;
}
first = left;
left = 0, right = n;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target) {
right = mid;
} else {
left = mid + 1;
}
}
last = left - 1;
return last - first + 1;
}
};
func search(nums []int, target int) int {
if len(nums) == 0 {
return 0
}
left, right := 0, len(nums)-1
for left < right {
mid := (left + right) >> 1
if nums[mid] >= target {
right = mid
} else {
left = mid + 1
}
}
if nums[left] != target {
return 0
}
l := left
right = len(nums) - 1
for left < right {
mid := (left + right + 1) >> 1
if nums[mid] <= target {
left = mid
} else {
right = mid - 1
}
}
return left - l + 1
}
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function(nums, target) {
if (nums.length == 0) {
return 0;
}
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
if (nums[left] != target) {
return 0;
}
let l = left;
right = nums.length - 1;
while (left < right) {
const mid = (left + right + 1) >> 1;
if (nums[mid] <= target) {
left = mid;
} else {
right = mid - 1;
}
}
return left - l + 1;
};