0
点赞
收藏
分享

微信扫一扫

LeetCode题解(1150):检查一个数是否在数组中占绝大多数(Python)


题目:​​原题链接​​(简单)

标签:数组、二分查找

解法

时间复杂度

空间复杂度

执行用时

Ans 1 (Python)

O ( N )

O ( 1 )

44ms (34.91%)

Ans 2 (Python)

O ( l o g N )

O ( 1 )

40ms (53.77%)

Ans 3 (Python)

解法一:

class Solution:
def isMajorityElement(self, nums: List[int], target: int) -> bool:
num = 0
for n in nums:
if n != target:
num -= 1
else:
num += 1

return num > 0

解法二(二分查找):

class Solution:
def isMajorityElement(self, nums: List[int], target: int) -> bool:
i1 = bisect.bisect_left(nums, target) # 目标值的第1个数
i2 = bisect.bisect_right(nums, target) # 比目标值大的第1个数
return i2 - i1 > len(nums) / 2



举报

相关推荐

0 条评论