LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到
思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置。
class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nums.end());
return nums[nums.size()/2];
}
};
线性解法:投票算法,多的票抵消了其余人的票,那么我的票一定还有剩的。
int majority;
int cnt = 0;
for(int i=0; i<num.size(); i++){
if ( cnt ==0 ){
majority = num[i];
cnt++;
}else{
majority == num[i] ? cnt++ : cnt --;
if (cnt >= num.size()/2+1) return majority;
}
}
return majority;
Majority Element II:
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
思路:
窗口(n/3)检查算法。先排序,然后用一个长度为n/3的窗口来检查两端的数是否相等。
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
int maj_ele;
int len=nums.size();
sort(nums.begin(),nums.end());
int index=;
while(index<len)
{
maj_ele = nums[index];
if(maj_ele == nums[index+len/])
{
res.push_back(maj_ele);
while(index<len && maj_ele == nums[++index])
;
}
else
{
index++;
}
}
return res;
}
};
LeetCode(169)Majority Element and Majority Element II的更多相关文章
- Leetcode # 169, 229 Majority Element I and II
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode之分治法专题-169. 求众数(Majority Element)
Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 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 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- ios开发逆向传值的几种方法整理
第一种:代理传值 第二个控制器: @protocol WJSecondViewControllerDelegate <NSObject> - (void)changeText:(NSStr ...
- 调整label中text显示的行间距
调整label中text显示的行间距最近再做一个项目时,发现UILabel中text的系统默认行间距不能满足要求,于是在网上找到了调整行间距的代码.跟大家分享一下,希望能对你有所帮助.悦德财富:htt ...
- SharePoint 2013 开发——开发自定义操作APP
博客地址:http://blog.csdn.net/FoxDave 自定义操作即我们所说的Ribbon和ECB(Edit Control Block),在SharePoint 2013之前,我们可以 ...
- 统计一段文字中出现频率最高的10个单词(c语言)
注:这次使用C语言做的这个程序.个别不懂的地方和算法部分是请教的其他同学,交流并吸收,所以收获颇多! 在程序中每一个地方我都做了注释,方便同学之间交流.也让老师容易看.程序也有很多不足的地方,但限于本 ...
- JS原生回到顶部效果
// 回到顶部 onload = function () { var oBtnTop = document.getElementById('toTop'); var timer = null; oBt ...
- WPF 基础到企业应用系列索引
转自:http://www.cnblogs.com/zenghongliang/archive/2010/07/09/1774141.html WPF 基础到企业应用系列索引 WPF 基础到企业应用系 ...
- Access数据库参数没值
OleDbParameter[] par = { new OleDbParameter(Par_Password,Info.Password), new OleDbParameter(Par_StuN ...
- iOS 关于UIWindow 的认识
UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWindow,接着创建控制器的view,最后将控制器的view ...
- Java容器类接口的选择
我们知道Java容器类实际提供了四类接口:Map,List,Set和Queue,如下图所示,每种接口都有不止一个版本的实现,如果在实际编写程序时需要使用某种接口时该如何选择. 从Oracle的Java ...
- vijos 1779 国王游戏
练了一下高精度..结果敲了这么久... #include<iostream> #include<cstdio> #include<cstring> #include ...