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. webpack操作基础

    webpack 是一个前端加载/打包工具,根据模块的依赖关系进行静态分析,并依根据规则生成对应的静态资源

  2. HTML5常见的面试题,基础知识点

                                                                                    HTML5常见的面试题 一.HTML 常 ...

  3. 2016 10 28考试 dp 乱搞 树状数组

    2016 10 28 考试 时间 7:50 AM to 11:15 AM 下载链接: 试题 考试包 这次考试对自己的表现非常不满意!! T1看出来是dp题目,但是在考试过程中并没有推出转移方程,考虑了 ...

  4. BZOJ 1057: [ZJOI2007]棋盘制作 悬线法求最大子矩阵+dp

    1057: [ZJOI2007]棋盘制作 Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑 ...

  5. [jzoj NOIP2018模拟10.29]

    OI生涯的最高分,来了纪中这么多天,在经历了这么多场“NOIP难度”的模拟赛之后,终于看到了真正的NOIP 今天考场上效率很高,很快码完了全部的题目,留下了足够的时间对拍和...发呆.不得不说看着电脑 ...

  6. HTML5学习笔记(二):用于构建页面的语义元素

    1.语义元素 大多数HTML5语义元素的用途是标识页面中的一个内容区块,为标注的内容赋予额外的含义,不真正做任何事. 2.使用语义元素的原因 容易修改和维护: 无障碍性(现代Web设计的一个重要主题) ...

  7. ubuntu18.04中安装iNode

    title: ubuntu18.04中安装iNode toc: false date: 2018-09-01 17:52:20 categories: methods tags: ubuntu iNo ...

  8. 关于JQuery中的事件冒泡

    什么是事件冒泡? 事件冒泡就是当父元素和子元素存在同一事件时在子元素的事件处理程序中会自动调用其父级元素的事件处理程序. demo: <!DOCTYPE html> <html xm ...

  9. Pyinstaller 1 使用PyInstaller

    使用PyInstaller pyinstaller命令的语法是: pyinstaller[ options ] script [ script ...] | spec文件 在最简单的情况下,将当前目录 ...

  10. spline interpolation and draw image by matplotlib

    1 # spline interpolation and draw image by matplotlib from scipy import interpolate import matplotli ...