229. Majority Element II My Submissions Question
Total Accepted: 23103 Total Submissions: 91679 Difficulty: Medium
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.
Hint:
How many majority elements could it possibly have?
Do you have a better hint? Suggest it!
最多只有两个候选数字。要求O(1)空间,可以分别用两个变量保存候选数字和计数,方法大致和169. Majority Element My Submissions Question相同。代码如下:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
if (nums.empty())
return res;
int cand1 = nums[0], cand2 = 0;
int count1 = 1, count2 = 0;
for (int i = 1; i < nums.size(); ++i) {
if (cand1 == nums[i]) {
++count1;
}
else if (cand2 == nums[i]) {
++count2;
}
else if (count1 == 0) {
cand1 = nums[i];
++count1;
}
else if (count2 == 0) {
cand2 = nums[i];
++count2;
}
else {
--count1;
--count2;
}
}
//判断候选是否满足条件
count1 = count2 = 0;
for (int i = 0; i < nums.size(); ++i) {
if (cand1 == nums[i])
++count1;
else if (cand2 == nums[i])
++count2;
}
if (count1 > nums.size() / 3)
res.push_back(cand1);
if (count2 > nums.size() / 3)
res.push_back(cand2);
return res;
}
229. Majority Element II My Submissions Question的更多相关文章
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 【刷题-LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- LeetCode OJ 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] 229. Majority Element II 多数元素 II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- leetcode 229 Majority Element II
这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...
随机推荐
- Linux学习笔记--(1)
今天用Linux 的 test 命令,发现了一个有趣的现象: 打入 " test "abc"="abc" ;echo $? " 后,结果应该 ...
- linux 查看当前路径命令:pwd
查看当前路径命令:pwd pwd命令能够显示当前所处的路径. 这个命令比较简单,如果有时在操作过程中忘记了当前的路径,则可以通过此命令来查看路径,其执行方式为: # pwd /home/samlee ...
- ArcGIS Add-in开发(一)--获取选定要素的属性值
刚刚接触AE开发,记录一下自己的学习心得! 欢迎大家一起交流探讨! 最近做大赛,突然想到可以让项目更加直观的操作,就在项目中加了幅底图(底图很简单) 我想在arcmap中选中相应的要素后,在后台通过写 ...
- 【转】2013年中国IT业10大公司
转自:http://www.chinaz.com/news/2013/1217/331446.shtml?zyy 1.最得志的公司:小米 在2013年,再没有一家公司像小米这样志得意满,即便看看所有的 ...
- Python 动态语言
1.在C++中,Animal a = Person(); 这样写是不行的,因为a的内容不能使用Person的内容来填充. 2.在Python中,变量不需要声明,而且可以赋任何值.Python是如何做到 ...
- 从零开始学android开发- layout属性介绍
android:id 为控件指定相应的ID android:text 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 android:gravity 指定Vi ...
- Codeforces Round #185 (Div. 2) B. Archer 水题
B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...
- Codeforces Gym 100114 H. Milestones 离线树状数组
H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descripti ...
- 2.目录:疯子讲iOS课程
目录:疯子讲iOS课程 写这个目录让我纠结好几天,一是我在考虑要不要从Objective-c基础开始讲,是否要使用ARC的方式讲,二是本人的游戏这几天正在封测,时间也比较紧张.纠结于有些朋友可能还不了 ...
- [Angular 2] Factory Provider with dependencies
This lesson discusses when and how to add dependencies, resolved by Angular’s DI, to factory provide ...