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++)的更多相关文章

  1. LeetCode169:Majority Element(Hash表\位操作未懂)

    题目来源: Given an array of size n, find the majority element. The majority element is the element that ...

  2. 169. Majority Element(C++)

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. LeetCode OJ:Majority Element(主要元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  6. 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 ...

  7. leetcode——169 Majority Element(数组中出现次数过半的元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【ArcEngine入门与提高】Element(元素)、Annotation(注记)旋转

    因项目需要,需要做一个旋转注记的工具.因为注记这玩意用的比较少,网上资源也很少,所以做起来相当头疼.在经过一番研究之后,终于搞清楚注记的存储原理了,原来是和Element的类似,只不过注记是要把Ele ...

  9. [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

随机推荐

  1. vi / vim 删除以及其它命令

    删除一行:dd 删除一个单词/光标之后的单词剩余部分:dw 删除当前字符:x 光标之后的该行部分:d$ 文本删除 dd 删除一行 d$ 删除以当前字符开始的一行字符 ndd 删除以当前行开始的n行 d ...

  2. 教程-脚本之Python

    1.百度python就可以下载到最新版本(Python3.3)     PS:我发现用2.7版本写的py文件用  Python3.3打开时,提示错误(print2.7不需要括号3.3必须加括号).2. ...

  3. 8-14-Exercise(博弈:HDU 1846 & HDU 1527 )

    B.HDU 1846    Brave Game 算是最简单的入门博弈题吧...... 呃......我用的......算是不是方法的方法吧——找规律~ 可以发现:X-M为奇数时,先手会输:而为偶数的 ...

  4. Asp.net 管道事件

    在处理该请求时将由 HttpApplication 类执行以下事件. 希望扩展 HttpApplication 类的开发人员尤其需要注意这些事件. 1. 对请求进行验证,将检查浏览器发送的信息,并确定 ...

  5. Mac 解决SSH登录服务器终端乱码

    一.Mac自带的终端 ssh 连接Linux 乱码,可用如下方法解决 终端 --> 偏好设置 --> 描述文件 --> 高级  --> 设为GBK 即可 二.secureCRT ...

  6. 在Java中如何用String类中的indexof方法得到一个词的出现频率

    public class Test{ public static void main(String[] args) { String s="hello jack hello look me ...

  7. 一秒钟速算(WINFORM)

    WIN7以上的系统可以直接运行,XP的系统要按装.NET 2.0 程序下载 http://files.cnblogs.com/xe2011/%E4%B8%80%E7%A7%92%E9%92%9F%E5 ...

  8. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  9. RHCA学习笔记:RH442-Unit9内核定时与进程延时

      Unit 9 Kernel Timing and Process Latency 内核定时与进程延时 学习目标: A.了解CPU 是怎样追踪时间的 B.调整CPU的访问次数 C.调整调度延时 D. ...

  10. -----------------------------MVC模式理解-----------------------------

    1.用户发送请求到控制层: 2.控制层将接受到的请求转发到模型层: 3.模型层将请求结果转发到控制层: 4.控制层将模型层返回的结果进行视图渲染: 5.视图渲染后返回(jstl): 6.将视图展示给用 ...