#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @param k int整型
# @return int整型
#
class Solution:
def GetNumberOfK(self, nums: List[int], k: int) -> int:
# write code here
# return nums.count(k)
ans, l, r = 0, 0, len(nums) - 1
while l < r:
if nums[l] == k:
ans += 1
l += 1
if nums[r] == k:
ans += 1
r -= 1
if l == r and nums[l] == k:
ans += 1
return ans



京公网安备 11010502036488号