[LeetCode] Majority Element
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.
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相关推荐
- LeetCode Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode]Majority Element
题目描述: Given an array of size n, find the majority element. The majority element is the element that ...
- [Leetcode] Majority Element 众数
Majority Element I Given an array of size n, find the majority element. The majority element is the ...
- [LeetCode] Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 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 ...
- LeetCode - Majority Element
参考了别人的想法,思路简洁,效率高.虽然是在充分理解别人的思路后写出的代码,但是还是发现了自己的不足之处.以后还是要多思考多写.加油吧! public class Solution {public i ...
- [leetcode]Majority Element II
思路:可以使用三向切分的快速排序算法,因为这种算法会把小于某个数,等于某个数,和大于某个数的分开,分成三部分.很容易判断等于某个数的全部数量,对于这种重复值问题,很有效. public class S ...
- 【LeetCode】169. Majority Element
原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
最新文章
- ActiveX控件和它的容器
- Python基础语法06--文件
- 15. 三数之和(双指针)
- 【今日头条】头条号图文发布页面的“扩展链接”是干嘛用的?
- 给3月要跳槽的前端提个醒!不了解微前端就别去面试了,不然……
- 利用矩阵的n次方求图的连通性
- SQLServer2000同步复制技术实现步骤作者
- Springboot集成SpringData JPA
- 数据架构总体设计方案
- 2022持续学习-架构相关
- LISA: A Learned Index Structure for Spatial Data
- emlog模板-蓝叶博客模板源码-清新大气模板,适合做博客和资源一体!亲测
- DRM DUMB相关说明
- 华为手机媒体音量自动静音_华为媒体音量自动静音
- Kubernetes:dashboard 搭建(k8s -web端管理)
- 无线电视服务器主机名,电视服务器主机名怎么填
- Mybatis 批量插入数据 SQL
- 解决Grails2.0-M1下 Intellij IDEA 中Controller等无法自动重新加载的BUG
- 网络空间安全-密码学
- Prometheus自带指标整理