原题网址; https://www.lintcode.com/problem/majority-element-ii/

描述

给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一。

数组中只有唯一的主元素

您在真实的面试中是否遇到过这个题?  是

样例

给出数组[1,2,1,2,1,3,3] 返回 1

挑战

要求时间复杂度为O(n),空间复杂度为O(1)。

查看标签

枚举法

贪心
 
思路:利用map,建立nums【i】与数量count 的映射,最后返回 count 大于元素个数三分之一的nums【i】。
 
AC代码:
class Solution {
public:
/*
* @param nums: a list of integers
* @return: The majority number that occurs more than 1/3
*/
int majorityNumber(vector<int> &nums) {
// write your code here
int n=nums.size();
map<int,int> m;
for (int i=;i<n;i++)
{
m.insert(pair<int,int>(nums[i],));
}
for (int i=;i<n;i++)
{
m[nums[i]]++;
}
for (int i=;i<n;i++)
{
if (m[nums[i]]>n/)
{
return nums[i];
}
}
}
};

另一种方法:摩尔投票法  可参照:摩尔投票法    https://blog.csdn.net/krystalhuang/article/details/71512901   以及 https://www.cnblogs.com/ZhangYushuang/p/4627503.html

思路:

1,   超过n/3的元素个数最多两个

2,   数组中连续3个数据为一组的话,一共n/3组,那么如果存在符合条件的元素,这个元素一定出现在某一个组内至少两次

3,   知道了以上两个条件后,用所谓的摩尔投票法,共两轮,

第一轮:找出出现次数最多的两个元素,每次存储两个元素n1和n2【注意n1 != n2】,如果第三个元素与其中一个相同,则增加计数cn1或cn2,  否则,清除n1和n2,从下一个数据开始重新统计.

根据第二条,数目较多的元素一定可以被查到,当然查到的不一定是大于n/3的.【有可能是连续3个为一组某个数字出现两~三次】

第二轮:验证这两个元素是否满足条件,即出现的次数是否大于n/3;

 AC代码:
class Solution {
public:
/*
* @param nums: a list of integers
* @return: The majority number that occurs more than 1/3
*/
int majorityNumber(vector<int> &nums) {
// write your code here
int n=nums.size();
int candidate1,candidate2,count1=,count2=;
for (int i=;i<n;i++)
{
if (count1==)
{
candidate1=nums[i];
count1=;
}
else if (count2==&&nums[i]!=candidate1) //注意此处的nums[i]!=candidate1,两个candidate不能是同一个数;
{
candidate2=nums[i];
count2=;
}
else if (nums[i]==candidate1)
{
count1++;
}
else if (nums[i]==candidate2)
{
count2++;
}
else
{
count1--;
count2--;
}
} count1=count2=;
for (int i=;i<n;i++)
{
if (nums[i]==candidate1)
{
count1++;
}
if (nums[i]==candidate2)
{
count2++;
}
} if (count1>n/)
{
return candidate1;
}
if (count2>n/)
{
return candidate2;
}
}
};
 
 此外,九章算法上也给出了本题的代码:https://www.jiuzhang.com/solutions/majority-number-ii/#tag-highlight-lang-cpp
 
该代码在lintcode网站上可以通过,但是放在VS2010下运行出错,原因是 candidate1 与 candidate2 未初始化,想不明白原因啊,费解。

47 Majority Element II的更多相关文章

  1. 47.Majority Element I & II

    Majority Element I 描述 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. You may assume that the array is non ...

  2. LeetCode(169)Majority Element and Majority Element II

    一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...

  3. Majority Element,Majority Element II

    一:Majority Element Given an array of size n, find the majority element. The majority element is the ...

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

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

  5. Majority Element(169) && Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...

  6. 【LeetCode】229. Majority Element II

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

  7. LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III

    LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...

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

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

  9. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

随机推荐

  1. Maven如何发布jar包到Nexus私库

    Nexus2可以通过管理界面来上传jar包到私库中,而最新的Nexus3却找不到了上传界面,只能通过以下方式来发布到私库. 发布第三方jar包 这种情况是maven远程仓库没有,本地有的第三方jar包 ...

  2. robotframework冷门关键字

    1.Reload Page 模拟页面重载 2.Register Keyword To Run On Failure 参数: Keyword 描述: 当Selenium2Library类库关键字执行失败 ...

  3. 数据库MySQL--常见基础命令

    基础命令: 查看所有数据库:show databases; 打开指定的数据库:use 库名: 查看当前库的所有表:show tables; 查看数据库其他库中的表:show tables from 库 ...

  4. Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.widget.CheckedTextView

    困扰了我一天啊 终于吧 这个大bug  给解决掉了 可能是 当时懵逼了  竟然忘记重新构造了!!尴尬了 直接把项目的 build  文件删除重新构造了一边!!

  5. 关于OpenLiveWriter出错的修补方法

    OpenLiveWriter使用一段时间后可能会打不开,提示错误如下: 这是只需要把电脑的.net更新到4.6以上版本就可以了.

  6. java 数组常见操作

    1.遍历  依次访问数组中的每个元素 public class ArrayDemo04 { public static void main(String[] args) { int[] arr = { ...

  7. JS继承(简单理解版)

    童鞋们,我们今天聊聊js的继承,关于继承,平时开发基本用不到,但是面试没有不考的,我就想问,这是人干的事吗? 好吧,迫于社会主义核心价值观,我们今天就来简单说一说js的继承,谁让它是面向对象编程很重要 ...

  8. QT之QStatusBar

    1.QStatusBar一般处于主窗体(QMainWindow)的左下角用于显示比较重要的状态信息.我们通常调用其showMessage()函数 QT官方显示:[slot] void QStatusB ...

  9. P1305 新二叉树 /// 二叉树的先序遍历

    题目大意: https://www.luogu.org/problemnew/show/P1305 由题目可知,输入首位为 子树的根 其后为其左右儿子 则除各行首位后的位置中 没有出现的那个字母肯定为 ...

  10. java_瞬时

    瞬时(Instant): 方法: public class InstantTest01 { public static void main(String[] args){ //静态方法,返回utc上的 ...