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. C#关于静态与非静态的区别

    C#静态方法与非静态方法的区别不仅仅是概念上的,那么他们有什么具体的区别呢?让我们通过本文向你做一下解析. C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用 ...

  2. 【android-cocos2d-X 环境配置】在Mac下搭建Cocos2d-X-android开发环境!

    转自:http://blog.csdn.net/dingkun520wy/article/details/17097593 (1)下载 首先要下载好要用到的东西: 1.android-SDK 地址是  ...

  3. 套题 Codeforces Round #277 (Div. 2)

    A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...

  4. ExpandableListView 箭头靠右

    ExpandableListView 默认标示箭头是在左边的,当左边有图片时,不是太好看,想把它放在右边,这么简单的事可我折腾死了,还好给我找到了. 参照了以下链接: expandableListvi ...

  5. MVC之 自定义过滤器(Filter)

    MVC之 自定义过滤器(Filter) 一.自定义Filter 自定义Filter需要继承ActionFilterAttribute抽象类,重写其中需要的方法,来看下ActionFilterAttri ...

  6. mkimage command not found

    转载:http://blog.csdn.net/armeasy/article/details/6217621 UIMAGE  arch/arm/boot/uImage"mkimage&qu ...

  7. IIS 发布 之 Word导出本地测试正常,发布报错

    用C#动态生成Word文档功能实现了,在本地的机器运行时是好的,但程序发布 IIS 或 远程服务器 上就报错, 报错信息为:检索 COM 类工厂中 CLSID 为 {000209FF-0000-000 ...

  8. JavaScript 关于this的理解

    this是一个挺神奇的东西,经常不知道它绑定到了那里 ,因此出来了各种绞尽脑汁的面试题. 例1 <script> var person={}; person.name='li'; pers ...

  9. ArcEngine中打开各种数据源(WorkSpace)的连接http://www.cnblogs.com/feilong3540717/archive/2011/08/07/2129906.html

    ArcEngine中打开各种数据源(WorkSpace)的连接 ArcEngine中打开各种数据源(WorkSpace)的连接 (SDE.personal/File.ShapeFile.CAD数据.影 ...

  10. DataTables warning (table id = 'myTable'): Requested unknown parameter '0' from the data source for row 0

    第一种方式:不用在js里设置列Html: <table id="myTable"> <thead> <tr> <th>Title-1 ...