LeetCode题解(1150):检查一个数是否在数组中占绝大多数(Python)
发布日期:2021-06-29 20:13:34
浏览次数:2
分类:技术文章
本文共 689 字,大约阅读时间需要 2 分钟。
题目:(简单)
标签:数组、二分查找
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N ) O(N) O(N) | O ( 1 ) O(1) O(1) | 44ms (34.91%) |
Ans 2 (Python) | O ( l o g N ) O(logN) O(logN) | O ( 1 ) O(1) 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
转载地址:https://dataartist.blog.csdn.net/article/details/110731027 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月21日 21时40分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
uni-app 浏览器测试
2019-04-30
dom - 12 种 node 类型
2019-04-30
Dart - 环境配置
2019-04-30
Dart - comment 注释
2019-04-30
Dart - variate 变量
2019-04-30
Dart - default 默认值
2019-04-30
Dart - default&const
2019-04-30
Dart - if else
2019-04-30
Dart - 循环控制
2019-04-30
Dart - switch
2019-04-30
Dart - break 和 continue
2019-04-30
Dart - import
2019-04-30
Dart - 类
2019-04-30
Dart - 类的成员变量
2019-04-30
javascript - 严格模式(use strict)
2019-04-30
eslint warnings 与 error
2019-04-30
eslint - for-direction 循环
2019-04-30
eslint - getter-return
2019-04-30
css ===== text-transform
2019-04-30