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 ...
随机推荐
- JAVA学习1
以前学过JAVA,但是长时间不用又给忘了,趁着还有时间回顾一下. 一切皆是对象.
- JVM-内存分配与回收策略
简单介绍一下Java技术体系下的Java虚拟机内存分配与回收策略. 1.对象优先在Eden分配 大多数情况下,对象在新生代Eden区中分分配.当Eden区已没有足够空间进行分配时,虚拟机将发起一次 ...
- SpringMvc中的反射
controller中的方法,是通过反射调用的 spring监控controller中的注解,当命令符合某个注解的时候,通过反射,找到这个注解对应的方法,然后调用,处理完成得到返回值,再根据这个返回值 ...
- 第八章 标准IO库
1.IO对象时不可复制或者赋值的:也就是说形参或者返回类型也不能为流类型.如果非要传递或者返回IO对象的的话,则必须传递或者返回指向对象的指针或者引用.如: ofstream &print( ...
- URAL 1671 Anansi's Cobweb (并查集)
题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> ...
- 获得servletContext的路径的方法
request.getSession().getServletContext().getRealPath("")或 System.getProperty("webapp. ...
- Spring中配置文件applicationContext.xml配置详解
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 强引用strong和弱引用weak的定义
1.强引用表示从属关系,引用对象拥有被引用的对象.弱引用则暗示引用对象不拥有被引用的对象.一个对象的寿命是由它被强引用多少次来决定的.只要对象还存在强引用,就不会释放该对象. 注意:但是对象之间的引用 ...
- BZOJ 2467 生成树
当(n-1)条中间的边:4^(n-1)*4*C(n-1,n). ......以此类推Σ. f[n]=Σ(i=0..n-1)4^(i+1)*(n-i)*C(n,i) =Σ(i=0..n-1)4^(i+1 ...
- C#_控件——DropDownList
1.html <asp:CheckBox ID="CheckBox11" runat="server" onclick="changecheck ...