leetcode169——Majority Element (C++)
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.
个人博客:http://www.cnblogs.com/wdfwolf3/。
题目比较好理解,在含有n个元素的数组中找出出现次数超过⌊n/2⌋的元素,假设数组不为空而且这个数是一定存在的。
1.moore-voting算法
这个算法就是为解决这个问题诞生的,主要思想就是找出一对不同的元素就去掉它们,最后剩下的一定是所找的元素。
class Solution {
public:
int majorityElement(vector<int>& nums) {
int result=nums[],count,i;
for(i=,count=;i<nums.size();i++)
{
count+=nums[i]==result?:-;
if(count>nums.size()/)
break;
if(count==)
{
count=;
result=nums[i+];
i++;
}
}
return result;
}
}
(16ms)
2.hash
遍历数组,利用hash方式记录元素出现的次数,当某个元素次数超过⌊n/2⌋时,即为我们要找的。
class Solution {
public:
int majorityElement(vector<int>& nums) {
unordered_map<int,int> m;
int i;
for(i=;i<nums.size();i++)
{
if(++m[nums[i]]>(nums.size()/))
return nums[i];
}
return nums[];
}
};
(28ms)
3.sorting
对数组进行排序,由于要找的元素超过半数,所以下标n/2的元素一定是它,这里总数奇偶不影响,可以自己举例验证一下。
class Solution {
public:
int majorityElement(vector<int>& nums) { //40ms
sort(nums.begin(),nums.end());
return nums[nums.size()/];
}
};
4.randomization
随机选取数组中的一个数,验证它是不是超过半数的数字。时间最快,有一半几率选中,当然最坏的情况比较糟糕,但这不重要,就像快速排序时间复杂度一样。
class Solution {
public:
int majorityElement(vector<int>& nums) {
srand(unsigned(time(NULL)));
int tmp,i,count;
while(true)
{
tmp=nums[rand()%nums.size()];
for(i=,count=;i<nums.size();i++)
{
if(tmp==nums[i])
count++;
if(count>nums.size()/)
return tmp;
}
}
}
};
(16ms)
5.bit manipulation
就是把数字都作为二进制处理,每个二进制位上的n个bit相加看是否大于n/2,大于的话这一位上是1,小于的话就是0,把32位都这么处理完就知道结果是多少了。
class Solution {
public:
int majorityElement(vector<int>& nums) {
int i,j,count,major=;
for(i=;i<;i++)
{
for(j=,count=;j<nums.size();j++)
{
if((nums[j]>>i&)==)
count++;
}
if(count>nums.size()/)
major+=(<<i);
}
return major;
}
}
(40ms)
6.分治
处理这种问题都可以尝试下分治方法,这道题也可以不过感觉太麻烦,就不写了。
leetcode169——Majority Element (C++)的更多相关文章
- LeetCode169:Majority Element(Hash表\位操作未懂)
题目来源: Given an array of size n, find the majority element. The majority element is the element that ...
- 169. Majority Element(C++)
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 OJ:Majority Element(主要元素)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...
- leetcode——169 Majority Element(数组中出现次数过半的元素)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【ArcEngine入门与提高】Element(元素)、Annotation(注记)旋转
因项目需要,需要做一个旋转注记的工具.因为注记这玩意用的比较少,网上资源也很少,所以做起来相当头疼.在经过一番研究之后,终于搞清楚注记的存储原理了,原来是和Element的类似,只不过注记是要把Ele ...
- [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
随机推荐
- Android Studio 导入项目错误
Gradle DSL method not found: 'Android()' 错误原因: android studio 引进项目时,自动查找本机是否有项目设置的SDK版本,若发现没有,我们会在pr ...
- proftpd的示例配置文件
# This is a basic ProFTPD configuration file (rename it to # 'proftpd.conf' for actual use. It estab ...
- storm-starter项目概述
storm-starter项目包含使用storm的各种各样的例子.项目托管在GitHub上面,其网址为: http://github.com/nathanmarz/storm-starter stor ...
- win32键盘记录 -- 自定义窗口类
最近学了些关于window api编程的知识,于是琢磨编写一些键盘记录器,能够把输入的按键输出到窗口内,并且实现窗口自动滚动. 封装窗口类使用了GWL_USERDATA字段来保存this指针,比较容易 ...
- 【Mysql学习笔记】浅析mysql的binlog
最近读一份关于“数据库事务故障恢复"的技术资料,发现对mysql的binlog的认识不够清楚,查阅mysql reference manual有所收获,作为笔记,记录于此. 1. What' ...
- 字符串匹配算法之Rabin-Karp算法
关键思想在于把输入的字符既看作图形符号,又看做数字,预处理算出模式P的d进制的值p,时间复杂度为Θ(m),让后针对n - m + 1个有效偏移s计算出相应的ts,这里是由于利用ts来计算ts+1,时间 ...
- 【设计模式】单例设计模式的N中Java实现方法
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17359719 特点 单例模式的特点: 1.只能有一个实例: 2.必须自己创建自己的一个实例 ...
- 关于css中z-index 的应用
我想很多人在应用中的会碰到这个问题,设置 z-index无效:无论设置为多高的数字都没有效果: 原因是在设置z-index之前必须满足一下两个条件: 1,给设置z-index的元素设置相应的定位值,p ...
- BAPI
MM模块 1. BAPI_MATERIAL_SAVEDATA 创建物料主数据 注意参数EXTENSIONIN的使用,可以创建自定义字段 例如:WA_BAPI_TE_MARA-MATERIAL = IT ...
- 在SCVMM2012R2中删除失去联系的VM GateWay
当VM Gateway失去联系,无法使用,直接删除GW,或者在VM Network中删除GW连接,均会出现如下错误提示: 错误(21426)对配置提供程序 4ee559f1-f479-480c-945 ...