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的更多相关文章

  1. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  2. 【刷题-LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  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 ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  8. leetcode 229 Majority Element II

    这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...

  9. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...

随机推荐

  1. Exchange模式功能

    Exchange模式: Outlook中的投票功能: 新建邮件--选项--使用投票按钮

  2. 【转】C++ static关键字

    原文:http://blog.csdn.net/hackbuteer1/article/details/7487694 C++的static有两种用法:面向过程程序设计中的static和面向对象程序设 ...

  3. Python 删除目录中特定文件

    代码如下,使用了递归: import sys currDir = sys.path[] import os def removeFile(dir,postfix): if os.path.isdir( ...

  4. Fedora 下 安装 chrome

    一.下载安装包,安装 1.去google 下载安装包 2.终端下    运行命令:    rpm -ivh google-chrome-stable_current_i386.rpm 3.  出现如下 ...

  5. 使用proguard混淆android代码

    当前是有些工具比方apktool,dextojar等是能够对我们android安装包进行反编译,获得源代码的.为了降低被别人破解,导致源代码泄露,程序被别人盗代替码,等等.我们须要对代码进行混淆,an ...

  6. Windows下codeblocks的安装与配置

    最近自己正好要使用codeblocks编写C++程序,安装好却发现无法编译,如果您也遇到相似问题,可以参考本文. 如果您已安装codeblocks,想转换成中文界面,直接参考Step 2. 如果您C程 ...

  7. px,em,rem的区别

    PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位: 3. Firefox能够调整px和em,rem,但是96%以上 ...

  8. 使用Java编写并运行Spark应用程序

    我们首先提出这样一个简单的需求: 现在要分析某网站的访问日志信息,统计来自不同IP的用户访问的次数,从而通过Geo信息来获得来访用户所在国家地区分布状况.这里我拿我网站的日志记录行示例,如下所示: 1 ...

  9. Redis 脚本

    Redis 脚本使用 Lua 解释器来执行脚本. Reids 2.6 版本通过内嵌支持 Lua 环境.执行脚本的常用命令为 EVAL. 语法 Eval 命令的基本语法如下: redis 127.0.0 ...

  10. STM32W108芯片的SWD在IAR7.30版本中不能用

    提示说0x20000B8不能读,When Clear soft RAM BP