229. Majority Element II 【M】【52】
2016-05-15 15:28
323 查看
Given an integer array of size n, find all elements that appear more than
The algorithm should run in linear time and in O(1) space.
Hint:
How many majority elements could it possibly have?
Do you have a better hint? Suggest it!
Subscribe to see which companies asked this question
还是用抵消的办法
开始的参数是0.5,通过了
后来改成1 ,也通过了……
⌊ n/3 ⌋times.
The algorithm should run in linear time and in O(1) space.
Hint:
How many majority elements could it possibly have?
Do you have a better hint? Suggest it!
Subscribe to see which companies asked this question
还是用抵消的办法
开始的参数是0.5,通过了
后来改成1 ,也通过了……
class Solution(object): def majorityElement(self, nums): a = '' #nums[0] b = '' #nums[1] ta = 0 tb = 0 l = len(nums) if l == 2: return list(set(nums)) times = l/3 for i in nums[:]: if i == a: ta += 1 elif i == b: tb += 1 elif ta <= 0: a = i ta = 1 elif tb <= 0: b = i tb = 1 else: ta -= 1 tb -= 1 res = [] if nums.count(a) > times: res += a, if a != b and nums.count(b) > times: res += b, return res
相关文章推荐
- IOS小知识点总结
- Binder学习笔记(八)—— 客户端如何组织Test()请求 ?
- NSLock/NSRecursiveLock/NSConditionLock/@synchronized 详细解释
- JS(JavaScript)插入节点的方法appendChild与insertBefore
- 蓝桥杯:排序
- 239. Sliding Window Maximum
- Android开发,30行代码集成图片轮播RollViewpager,你说快吗?
- GridView中日期不显示时分秒的完美解决方法
- 封装类:即8种基本类型对应8种封装类
- iOS开发系列—Objective-C之Foundation框架
- C还可以这么玩第二期
- FromBottomToTop第十一周项目博客
- bootstrap table 显示行号
- c语言中包含math.h的时用gcc编译要加-lm参数
- Binder学习笔记(七)—— ServiceManager如何响应addService请求 ?
- iOS @synchronized(self) 解释
- ListView中常用Adapter
- 代理服务器与网络地址转换NAT
- <<深入Java虚拟机>>-第二章-Java内存区域-学习笔记
- Binder学习笔记(六)—— binder服务端是如何组织addService数据的?