【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 ...
随机推荐
- hdu 4588 Count The Carries
思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...
- hdu 4277 USACO ORZ
没什么好方法,只能用dfs了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> #inclu ...
- SpingMVC ModelAttribute的用法
@Controller @RequestMapping(value = "/test") public class TestController { @RequestMapping ...
- MySQL 建表字段长度的限制
脑补,varchar(N),N指的是最大字符数,不是字节数. 先上测试说明: 在MySQL建表时,遇到一个奇怪的现象: root@localhost : test 10:30:54>CREA ...
- java中什么时候该用static修饰方法?有什么好处或者坏处?
当一个方法或者变量需要初始化加载,或者是经常被调用的时候可以加上static.用static修饰的方法可以用类名直接调用,不用的一定要先实例化一个对象然后才可以调用比如 person这个类里面有一个方 ...
- Xamarin.Android 入门之:xamarin使用webserver和html交互
一.引言 如今,Android+html5开发已经成为最流行的开发模式. Android 中可以通过webview来实现和js的交互,在程序中调用js代码,只需要将webview控件的支持js的属性设 ...
- React 性能调优原理
一.React影响性能的两个地方 二.调优原理
- idea15 如何设置代码不自动折叠
IDEA默认情况下,如果方法体中只有一行,这个方法体会折叠起来,就像这种
- C++:析构函数
析构函数的特点: 1.析构函数与类名相同,但它前面必须加上波浪号~ 2.析构函数不返回任何值,在定义析构函数时,是不能说明它的类型的,甚至说明void类型也不能 3.析构函数没有参数,因此不能被重载. ...
- get Status canceled 请求被取消
1.chrome浏览器下状况: 2.环境: 一个页面A下 包含一个 iframe ,在子页面中用js点击A页面下的链接替换iframe内容脚本如下: window.parent.document.ge ...