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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags

Divide and Conquer Array Bit Manipulation


这题是简单的一道遍历问题。
#include <iostream>
#include <vector>
using namespace std;class Solution {
public:int majorityElement(vector<int> &num) {int n = num.size();int cnt = 1;int nowInt = num[0];for(int i =1;i<n;i++){if(num[i]==nowInt){cnt++;continue;}cnt--;if(cnt<0){cnt = 1;nowInt = num[i];}}return nowInt;}
};int main()
{vector<int> num ={1,2,4,4,1,2,2,3,1,1,1,1,1,1,1,1};Solution sol;cout<<sol.majorityElement(num)<<endl;return 0;
}

转载于:https://www.cnblogs.com/Azhu/p/4323553.html

[LeetCode] Majority Element相关推荐

  1. LeetCode Majority Element

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

  2. [LeetCode]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  3. [Leetcode] Majority Element 众数

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

  4. [LeetCode] Majority Element II

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

  5. LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)

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

  6. LeetCode - Majority Element

    参考了别人的想法,思路简洁,效率高.虽然是在充分理解别人的思路后写出的代码,但是还是发现了自己的不足之处.以后还是要多思考多写.加油吧! public class Solution {public i ...

  7. [leetcode]Majority Element II

    思路:可以使用三向切分的快速排序算法,因为这种算法会把小于某个数,等于某个数,和大于某个数的分开,分成三部分.很容易判断等于某个数的全部数量,对于这种重复值问题,很有效. public class S ...

  8. 【LeetCode】169. Majority Element

    原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...

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

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

最新文章

  1. ActiveX控件和它的容器
  2. Python基础语法06--文件
  3. 15. 三数之和(双指针)
  4. 【今日头条】头条号图文发布页面的“扩展链接”是干嘛用的?
  5. 给3月要跳槽的前端提个醒!不了解微前端就别去面试了,不然……
  6. 利用矩阵的n次方求图的连通性
  7. SQLServer2000同步复制技术实现步骤作者
  8. Springboot集成SpringData JPA
  9. 数据架构总体设计方案
  10. 2022持续学习-架构相关
  11. LISA: A Learned Index Structure for Spatial Data
  12. emlog模板-蓝叶博客模板源码-清新大气模板,适合做博客和资源一体!亲测
  13. DRM DUMB相关说明
  14. 华为手机媒体音量自动静音_华为媒体音量自动静音
  15. Kubernetes:dashboard 搭建(k8s -web端管理)
  16. 无线电视服务器主机名,电视服务器主机名怎么填
  17. Mybatis 批量插入数据 SQL
  18. 解决Grails2.0-M1下 Intellij IDEA 中Controller等无法自动重新加载的BUG
  19. 网络空间安全-密码学
  20. Prometheus自带指标整理

热门文章

  1. python post提交数据
  2. SQL LEFT JOIN 关键字
  3. syslog(),closelog()与openlog()--日志操作函数
  4. 图解:人性的7种兵器(互联网商业逻辑)
  5. Mysql —— C语言链接mysql数据库,实现可以增删改查的角色权限登录系统
  6. 最常用git命令汇总(参考列表)
  7. CentOS忘记密码
  8. MFC接收ShellExecute多个参数
  9. luogu p1459 三值的排序
  10. flowJS源码个人分析