Given an array of size n, find the majority element. The majority element is the element that appears more than 
n/2 ⌋
 times.

You may assume that the array is non-empty and the majority element always exist in the array.


求主元素:包括n个元素的数组中。假设一个元素的出现次数大于n/2(向下取整)次。则该元素为主元素。

依据定义,则每两个数中必有一个是主元素,设定一个变量cnt=0。当两个数同样时。cnt++,不同一时候,cnt--。用变量
majority来记录主元素,每当cnt=0时,把当前元素赋给majority。成为候选的主元素。这样一来。最后的majority的值一定就是主元素。



代码例如以下:
class Solution {
public:
int majorityElement(vector<int> &num) {
int majority;
int cnt = 0;
for(int i=0; i<num.size(); i++){
if ( cnt ==0 ){
majority = num[i];
cnt++;
}else{
majority == num[i] ? cnt++ : cnt --;
if (cnt > num.size()/2) return majority;
}
}
return majority; }
};











Majority Element:主元素的更多相关文章

  1. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

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

  3. 【算法31】寻找数组的主元素(Majority Element)

    题外话 最近有些网友来信问我博客怎么不更新了,是不是不刷题了,真是惭愧啊,题还是在刷的,不过刷题的频率没以前高了,看完<算法导论>后感觉网上很多讨论的题目其实在导论中都已经有非常好的算法以 ...

  4. 主元素问题 Majority Element

    2018-09-23 13:25:40 主元素问题是一个非常经典的问题,一般来说,主元素问题指的是数组中元素个数大于一半的数字,显然这个问题可以通过遍历计数解决,时间复杂度为O(n),空间复杂度为O( ...

  5. LeetCode OJ:Majority Element II(主元素II)

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

  6. lintcode 中等题:majority number III主元素III

    题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...

  7. lintcode 中等题:Majority number II 主元素 II

    题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...

  8. 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

    题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...

  9. majority element(数组中找出出现次数最多的元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. 洛谷 P2507 [SCOI2008]配对

    P2507 [SCOI2008]配对 题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值 ...

  2. jQuery判断浏览器类型和版本

    jquery 判断浏览器类型 例:   if($.browser.msie) { alert("这是一个IE浏览器"); }else if($.browser.opera) { a ...

  3. [Unit Testing] Configure the Angular CLI to use the Karma Mocha test reporter

    Every Angular CLI generated project comes already with Karmapreinstalled as well a couple of executa ...

  4. iOS开发中的NSDateFormatter日期格式解析总结

    在工作中,常常遇到将时间解析出来转换成自己相应要求的时间格式,之前也有收集相应的转换格式,如今将自己收集的一部分了做个分享,应该比較完好了,欢迎大家继续补充 年 y 将年份 (0-9) 显示为不带前导 ...

  5. Unique path ii

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. Mosquito的优化——epoll优化(七)

    本文由逍遥子撰写,转发请标注原址: http://blog.csdn.net/houjixin/article/details/46413583 或 http://houjixin.blog.163. ...

  7. moveToThread的根本目的还是为了处理QObject的事件循环(如果为空指针的话,当前对象的所有的事件都不处理了),看官方说明就知道了

    Changes the thread affinity for this object and its children. The object cannot be moved if it has a ...

  8. [jzoj 4668] [NOIP2016提高A组模拟7.19] 腐败 解题报告(质数分类+慢速乘)

    题目链接: http://172.16.0.132/senior/#main/show/4668 题目: 题解: 考虑把A数组里的每个元素分解质因数,对于每个质因数开一个vector存一下包含这个质因 ...

  9. iframe 高度宽度自适应

    <iframe id="iframeHome" name="iframeHome" src="/Page/NewHome/GongZuoTai. ...

  10. UISrcoll控件简单介绍

    UISrcoll控件,简单的说就是让界面滑动 当使用uiimageview的时候,给控件设置图片素材时,图片的大小会根据控件的大小,自动做缩放 当使用uibutton的时候,如果是设置背景图,name ...