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 ...
随机推荐
- Chrome 跨域调试
1.关闭chrome浏览器(全部) 我们可以通过使用chrome命令行启动参数来改变chrome浏览器的设置,具体的启动参数说明参考这篇介绍.https://code.google.com/p/xia ...
- Openerp图片路径处理
Openerp目前存储图片如人力资源头像图片等都是以二进制的方式存储在数据库中,若要修改数据库里只存储路径可以用这种方法 Image 装饰器: Image装饰器包含3中图片显示 Image 大图片 i ...
- 【Python3 爬虫】03_urllib.error异常处理
urllib.error可以接受来自urllib.request产生的异常.urllib.error有两个方法:①URLError ②HTTPError URLError URLError产生的原因 ...
- 【Excle】文本日期转化为日期格式
现存在一列文本格式的日期 需要将该列转化为日期格式 方法一:使用分列 数据→分列,第三步选择[日期] 方法二:使用text函数 公式得到的结果为: 但是这样转化后的是文本型日期,需要转化为日期型得先转 ...
- android-pull方式解析xml文件以及XML文件的序列化
android解析XML ---------------------------基础要像磐石 在android平台上可以使用SAX.DOM和自带的Pull解析器解析xml文件,本文主要介绍使用pull ...
- Linux 常用命令速查
0x001 .在指定文件夹下递归查询包含一个字符串的文件(列出的文件内容片段) grep -r “要查找的串” 文件路径 如 : grep -r "helloworld&q ...
- \r与\n有何差别,编码的时候应该怎样使用
差别: \r: 全称:carriage return (carriage是"字车"的意思.打印机上的一个部件) 简称:return 缩写:r ASCII码:13 作用:把光标移动到 ...
- ubuntu 12.04上安装sougou输入法
1.卸载ibus sudo apt-get install ibus 2.添加源 sudo add-apt-repository ppa:fcitx-team/nightly 3.更新源 sudo a ...
- 一个关于原生 js 开发一款插件的前端教程
教程链接: http://www.codeasily.net/course/plugin_course/ 写的不是很好,前面比较松后面比较急,请大家见谅,本人也没多少年前端经验,拿以前写过的教程网站, ...
- Flex Air应用程序更改任务栏图标
Air应用程序中相关图标更改的方法: 1.安装后的应用程序图标和运行时的任务栏图标 这两个是一起的,通过更改配置文件application.xml中的icon节点,分别针对不同大小进行设置,未设置的会 ...