【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
思路:
【LeetCode 169】Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛。因此,接着上一题的方法做,只不过这回要投两个票啦,而且最后还得检查这两个投票结果是不是真的满足都超过三分之一,因为这一题题目什么都没有保证,所以答案可能有0个、1个、2个。
C++:
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> ret;
int len = nums.size();
if(len == )
return ret;
int m = , n = , cm = , cn = ;
for(int i = ; i < len; i++)
{
int val = nums[i];
if(m == val)
cm++;
else if(n == val)
cn++;
else if(cm == )
{
m = val;
cm = ;
}
else if(cn == )
{
n = val;
cn = ;
}
else
{
cm--;
cn--;
}
}
cm = cn = ;
for(int i = ; i < len; i++)
{
if(nums[i] == m)
cm++;
else if(nums[i] == n)
cn++;
}
if(cm * > len)
ret.push_back(m);
if(cn * > len)
ret.push_back(n);
return ret;
}
};
Python:
class Solution:
# @param {integer[]} nums
# @return {integer[]}
def majorityElement(self, nums):
m, n, cm, cn = 0, 0, 0, 0
ret = [] for val in nums:
if m == val:
cm = cm + 1
elif n == val:
cn = cn + 1
elif cm == 0:
m = val
cm = 1
elif cn == 0:
n = val
cn = 1
else:
cm = cm - 1
cn = cn - 1 cm, cn = 0, 0 for val in nums:
if m == val:
cm = cm + 1
elif n == val:
cn = cn + 1 if cm * 3 > len(nums):
ret.append(m)
if cn * 3 > len(nums):
ret.append(n) return ret
【LeetCode 229】Majority Element II的更多相关文章
- 【LeetCode 169】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode OJ】Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that app ...
- 【数组】Majority Element II
题目: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The alg ...
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
随机推荐
- Genymotion加载so出错解决方案
通过网上所搜得出结论: Genymotion是x86的架构,而我们的so库是arm架构的 解决:安装Genymotion-ARM-Translation.zip 1.下载:http://pan.bai ...
- Fragment 与 Activity 通信
先说说背景知识: (From:http://blog.csdn.net/t12x3456/article/details/8119607) 尽管fragment的实现是独立于activity的,可以被 ...
- *IDEA真好用
使用maven做开发,在编辑pom.xml文件时
- ArcGIS 10 影像去黑边
在作卫片执法项目中,需要多个影像叠加截图,这就会出现影像黑边叠加的情况,这时就需要对多幅影像进行处理.主要有两种处理方式:以ArcGIS10.1为例,操作如下: 1.acrtoolbox——& ...
- Java API —— Random类
1.Random类概述 此类用于产生随机数 如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列. 2.构造 ...
- openfire的smack和asmack
smack你可以看成是一套封装好了的用于实现XMPP协议传输的API,它是一个非常简单并且功能强大的类库,给用户发送消息只需要三行代码.下载地址:http://www.igniterealtime.o ...
- Xcode学习
http://www.cnblogs.com/ygm900/p/3488881.html
- 【设计模式】—— 单例模式Singleton
前言:[模式总览]——————————by xingoo 模式意图 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有公开的调用方法. 模式结构 ...
- Python之格式化输出讲解
1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...
- UVALive 3211 Now or later(2-sat)
2-sat问题,一种在两种可能性中选择必然关系的问题. 推荐两篇论文,也是学2-sat公认比较好的材料.前者较好理解,后者需耐心看. http://www.google.com.hk/url?sa=t ...