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 ...
随机推荐
- 计算智能 Computational Intelligence,CI
计算智能(Computational Intelligence,CI)是借助自然界(生物界)规律的启示,根据其规律,设计出求解问题的算法.智能计算只是一种经验化的计算机思考性程序. 计算智能算法主要包 ...
- 【Java基础】Java多线程之线程组和线程池
在上一篇文章中,讲述了线程的基本概念和用法,这里将继续讲述线程组和线程池的一些东西. 线程组:java.lang.ThreadGroup 1. 线程组的介绍 线程组表示一个线程的集合.此外,线程组也可 ...
- HW5.7
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW1.4
public class Solution { public static void main(String[] args) { System.out.println("a a^2 a^3& ...
- Win8关机 一直重启的问题 解决方案-摘自网络
win8关机时自动重启的解决方法:关闭快递启动,默认是启动的,到控制面板 – 电源选项 – 选择电源按钮的功能 – 更改当前不可用的设置 – 关机设置 – 把勾去掉 关闭快速启动.这样就不会关机一直重 ...
- OS X: Keyboard shortcuts
Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a char ...
- Hadoop源码的编译过程详细解读(各版本)
说在前面的话 重新试多几次.编译过程中会出现下载某个包的时间太久,这是由于连接网站的过程中会出现假死,按ctrl+c,重新运行编译命令. 如果出现缺少了某个文件的情况,则要先清理maven(使用命 ...
- Video Surveillance - POJ 1474(判断是否存在内核)
题目大意:询问是否在家里装一个监视器就可以监控所有的角落. 分析:赤裸裸的判断多边形内核题目. 代码如下: #include<iostream> #include<string.h& ...
- PHP 函数:intval()
intval 变量转成整数类型. 语法: int intval(mixed var, int [base]); 返回值: 整数 函数种类: PHP 系统功能 内容说明:本函数可将变量转成整数类型. ...
- 小编接地气——第六届中国云计算大会攻略Q&A
2014年5月20-23日,第六届中国云计算大会在北京召开. 花个1000多元,在工作日请假来參加大会,不能让大家白跑一趟而是物有所值. 小编写了大会攻略Q&A,分享给各位 Q:为什么要參加关 ...