描述: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.

思路1:Moore voting algorithm--每找出两个不同的element,就成对删除即count--,最终剩下的一定就是所求的。时间复杂度:O(n)

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int> &num) {
  4.  
  5. int elem = ;
  6. int count = ;
  7.  
  8. for(int i = ; i < num.size(); i++) {
  9.  
  10. if(count == ) {
  11. elem = num[i];
  12. count = ;
  13. }
  14. else {
  15. if(elem == num[i])
  16. count++;
  17. else
  18. count--;
  19. }
  20.  
  21. }
  22. return elem;
  23. }
      };

思路2:随机挑选一个元素,检查是否是多数元素。时间复杂度:Average:O(n)。期望查找次数 <2

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int> &num) {
  4.  
  5. int count = ;
  6.  
  7. for(;;) {
  8. if(num.size() == )
  9. return num[];
  10. else {
  11. int i = rand() % (num.size() - );
  12. for(int j = ; j < num.size(); j++) {
  13. if(num[j] == num[i])
  14. count++;
  15. }
  16. if(count > (num.size() / ))
  17. return num[i];
  18. else {
  19. count = ;
  20. continue;
  21. }
  22. }
  23. }
  24. }
      };

附LeetCode建议解决方案:

LeetCode Problem 169: Majority Element查找多数元素的更多相关文章

  1. [LeetCode&Python] Problem 169. Majority Element

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

  2. LeetCode OJ 169. Majority Element

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

  3. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

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

  4. 【LeetCode】169 - Majority Element

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

  5. 【一天一道LeetCode】#169. Majority Element

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. LeetCode【169. Majority Element】

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

  7. [LeetCode] 162. Find Peak Element 查找峰值元素

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

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

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

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

随机推荐

  1. Java中的值传递与“引用传递”

    首先,Java没有 引用传递 这么一说. Java仅仅有值传递.传递的都是值,基本数据类型传递的是值.引用类型传递的是地址值. 我来理一下这当中宛如米线跟米面绞在一起了,再跟粉丝混在一起的关系. 好的 ...

  2. JAVA加解密 -- 数字签名算法

    数字签名 – 带有密钥的消息摘要算法 作用:验证数据完整性.认证数据来源.抗否认(OSI参考模型) 私钥签名,公钥验证 RSA 包含非对称算法和数字签名算法 实现代码: //1.初始化密钥 KeyPa ...

  3. oracle调优 浅析有效的游标管理

    浅析有效的游标管理 [思路分析] 能够把游标理解成共享的运行计划,当sql不被共享时.常规的解决思路有两个方向: 1.调整共享池的尺寸(共享池的库缓存区中共享运行计划): 2.sql书写时尽量重用绑定 ...

  4. npm install --no-bin-links中的参数“no-bin-links”表示什么意思

    npm install --no-bin-links中的参数"no-bin-links"表示什么意思

  5. grid 布局一 固定宽度+自适应宽度

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  6. linux-c 调试 gdb

    GDB(GNU Debugger) gcc -g –o testarg testarg.c //可执行文件中带上调试信息,用于后续的gdb调试 gdb testarg l; list //显示源程序 ...

  7. windowns 查看端口占用

    开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...

  8. Linux如何关机与关机命令祥解

    Linux关机命令祥解 1.直接关电源 2.init 0 3.telinit 0 4.shutdown -h now 5.halt6.poweroff 1.shutdown shutdown命令安全地 ...

  9. 可以打开QQ,但打不开网页的DNS服务器设置问题

    方法二: IE->设置->连接->局域网设置

  10. MVC | 微软自带的Ajax请求

    @Ajax.BegForm( )  用来生成异步表单 Home控制器 using System; using System.Collections.Generic; using System.Linq ...