统计一个数字在排序数组中出现的次数。
示例 1:
输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
示例 2:
输入: nums = [5,7,7,8,8,10], target = 6
输出: 0
提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums 是一个非递减数组
-109 <= target <= 109
解法1:暴力法遍历数组,出现一次计数一次
class Solution:
def search(self, nums: List[int], target: int) -> int:
cnt = 0
for i in nums:
if i == target:
cnt += 1
return cnt
解法2:找出target的索引,计算索引个数
class Solution:
def search