LeetCode Problem 169: 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.
思路1:Moore voting algorithm--每找出两个不同的element,就成对删除即count--,最终剩下的一定就是所求的。时间复杂度:O(n)
- class Solution {
- public:
- int majorityElement(vector<int> &num) {
- int elem = ;
- int count = ;
- for(int i = ; i < num.size(); i++) {
- if(count == ) {
- elem = num[i];
- count = ;
- }
- else {
- if(elem == num[i])
- count++;
- else
- count--;
- }
- }
- return elem;
- }
};
思路2:随机挑选一个元素,检查是否是多数元素。时间复杂度:Average:O(n)。期望查找次数 <2
- class Solution {
- public:
- int majorityElement(vector<int> &num) {
- int count = ;
- for(;;) {
- if(num.size() == )
- return num[];
- else {
- int i = rand() % (num.size() - );
- for(int j = ; j < num.size(); j++) {
- if(num[j] == num[i])
- count++;
- }
- if(count > (num.size() / ))
- return num[i];
- else {
- count = ;
- continue;
- }
- }
- }
- }
};
附LeetCode建议解决方案:
LeetCode Problem 169: Majority Element查找多数元素的更多相关文章
- [LeetCode&Python] Problem 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode OJ 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 【LeetCode】169 - Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【一天一道LeetCode】#169. Majority Element
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode【169. Majority Element】
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [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 ...
随机推荐
- Java中的值传递与“引用传递”
首先,Java没有 引用传递 这么一说. Java仅仅有值传递.传递的都是值,基本数据类型传递的是值.引用类型传递的是地址值. 我来理一下这当中宛如米线跟米面绞在一起了,再跟粉丝混在一起的关系. 好的 ...
- JAVA加解密 -- 数字签名算法
数字签名 – 带有密钥的消息摘要算法 作用:验证数据完整性.认证数据来源.抗否认(OSI参考模型) 私钥签名,公钥验证 RSA 包含非对称算法和数字签名算法 实现代码: //1.初始化密钥 KeyPa ...
- oracle调优 浅析有效的游标管理
浅析有效的游标管理 [思路分析] 能够把游标理解成共享的运行计划,当sql不被共享时.常规的解决思路有两个方向: 1.调整共享池的尺寸(共享池的库缓存区中共享运行计划): 2.sql书写时尽量重用绑定 ...
- npm install --no-bin-links中的参数“no-bin-links”表示什么意思
npm install --no-bin-links中的参数"no-bin-links"表示什么意思
- grid 布局一 固定宽度+自适应宽度
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- linux-c 调试 gdb
GDB(GNU Debugger) gcc -g –o testarg testarg.c //可执行文件中带上调试信息,用于后续的gdb调试 gdb testarg l; list //显示源程序 ...
- windowns 查看端口占用
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- Linux如何关机与关机命令祥解
Linux关机命令祥解 1.直接关电源 2.init 0 3.telinit 0 4.shutdown -h now 5.halt6.poweroff 1.shutdown shutdown命令安全地 ...
- 可以打开QQ,但打不开网页的DNS服务器设置问题
方法二: IE->设置->连接->局域网设置
- MVC | 微软自带的Ajax请求
@Ajax.BegForm( ) 用来生成异步表单 Home控制器 using System; using System.Collections.Generic; using System.Linq ...