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 ...
随机推荐
- .net错误处理机制
原地址:http://blog.csdn.net/lxbg90058/article/details/5651767 没有不出错的软件 从不出错的软件从某种程度上讲是不可能的! 和普通人的观念相反,创 ...
- Unity Shader 效果学习
Unity上对于图像的处理,假设单纯使用代码.那么非常遗憾,程序基本会跑死,毕竟是直接对像素的操作,读取写入都是比較耗费CPU和内存的. 所以.这次由于项目须要想实现类似哈哈镜的效果.想来想去,还是认 ...
- odoo8.0条形码改为js方式处理
群里网友@上海-gavin 提供的odoo条形码处理,将原来的图片生成方式改为js处理方式. <div class="row text-center"> <div ...
- 基于Jekyll的博客模板
代码地址如下:http://www.demodashi.com/demo/13147.html 效果 环境配置 环境 Windows 10 Git Bash 安装ruby 下载rubyinstalle ...
- Java之旅(2)—反射
1. 概念 反射就是将java类中的各种成分映射成对应的java类.之前我们已经讲过了Class类,也明确了一个java类中用一个Class类的对象来表示,一个类中的组成部分有:成员变量,方法 ...
- lucene ParallelMultiSearcher与MultiSearcher的区别
http://www.cnblogs.com/twilight/archive/2009/10/09/1579793.html ParallelMultiSearcher与MultiSearcher的 ...
- 【MyBatis学习15】MyBatis的逆向工程生成代码
1. 什么是逆向工程 mybatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需 ...
- 【MyBatis学习11】MyBatis中的延迟加载
1. 什么是延迟加载 举个例子:如果查询订单并且关联查询用户信息.如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息.把对用户信息的按需去查询就是延迟加载. 所以延迟加载即先从单表 ...
- Quartz.net基于数据库的任务调度管理(Only.Jobs)
一 前言: 各大调度组件优缺点在这就不讨论了,使用Quartz.net是因为它可以执行秒级任务. Only.Jobs 项目通过将各Job存储在数据库中,启动一个专门的Job管理任务来循环调度各Job的 ...
- DNS的概念,用途,DNS查询的实现算法
1.DNS的概念,用途 DNS是由解析器以及域名服务器组成的. 域名服务器是指保存有该网络中所有主机的域名和对应IP地址,并具有将域名转换为IP地址功能的服务器. DNS ...