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 ...
随机推荐
- linux后台启动命令-的原因
linux后台启动命令-的原因 学习了:https://blog.csdn.net/saife/article/details/78276014 学习了:https://www.ibm.com/dev ...
- 在线安装eclipse中html/jsp/xml editor插件 eclipseeditor
1.打开eclipse中的help————>Install New Software 2.点击Add按钮,然后弹出一个框,第一个文本框可以随便写,第二个一定要写: http://download ...
- HDU 5379 Mahjong tree(dfs)
题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...
- Python 类的特殊成员方法详解
class doges(object): """类的描述信息""" def __init__(self,name,food): self.n ...
- @Autowired与@Resource的使用方法和差别
一.@Autowired: 1.Spring 2.5 引入了 @Autowired 凝视,它能够对类成员变量.方法及构造函数进行标注,完毕自己主动装配的工作. 通过 @Autowired的使用来消除 ...
- XAOP的使用示范例子
代码地址如下:http://www.demodashi.com/demo/12976.html XAOP 一个简易的AOP(Android)应用框架.囊括了最实用的AOP应用. 特点 支持快速点击切片 ...
- Cookie的增删改查
增加: 第一种方法:Response.Cookies[“UserName”].Value=”张三” Response.Cookies[“UserName”].Expires=DateTime.Now. ...
- wamp通过phpMyAdmin修改登录密码
初始安装wamp后,默认mysql是没有密码的,这个时候如果想要修改密码,可以按照以下步骤进行: 第一.打开phpMyAdmin,看到界面如图所示: 第二.通过导航找到“用户”,再找到“编辑权限”进行 ...
- Visual Studio提示“无法启动IIS Express Web服务器”或者“无法连接Web服务器IIS Express ”的解决方法
解决办法:找到程序根目录,删除隐藏的.vs文件夹即可. 问题原因:一般是项目拷贝或者系统设置变更所造成的.
- 【CODEFORCES】 C. Captain Marmot
C. Captain Marmot time limit per test 1 second memory limit per test 256 megabytes input standard in ...