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. 判断是否已安装.net framework

    1.检测 %SystemRoot%\System 目录下的MSCorEE.dll文件,如果存在,则表明.net framework 已安装. 2.检测一下注册表项的子项: KEY_LOCAL_MACH ...

  2. 【Java基础】Java异常的一些总结

    什么是异常 异常是指程序运行可能出现的不能正常继续的情况,也可以理解为程序出现了不在预期范围内的一些情况,都可以称之为异常. 异常的分类 所有的异常类是从java.lang.Exception类继承的 ...

  3. XXX 用户 is not in the sudoers file. This incident will be reported 的问题解决方案

    说的是,这种问题,是出现在ubuntu系统里. root@SparkSingleNode:/usr/local/jdk# pwd /usr/local/jdk root@SparkSingleNode ...

  4. Powerdesigner设置表结构对齐方式

  5. 理解C# Attribute

    1.Attribute与Property Attribute是特性,Property是属性. 2.Attribute与注释 注释:是给程序员看的,编译的时候会去掉这些信息,也就是说,程序集中没有注释的 ...

  6. 【17】以独立语句将newed对象置入智能指针

    1.为什么? 考虑下面的情况:方法声明为void processWidget(shared_ptr<Widget> pw,int priority). 调用方法 processWidget ...

  7. Derby使用2—C/S模式

    零.回顾 这部分先来回顾一下上一篇博客中的主要内容.上一篇博客中主要简单介绍了Derby数据的历史,特点,安装以及使用的两种模式.这篇文章主要介绍这两种模式中的一种模式 一.启动服务端程序 第一部分主 ...

  8. jquery选择器及效率问题

    $('p2') //选择名字 $('.class') //选择class $('#id') //选择id $('#id li') //所有id=”id”标签内的li标签 $(“#id”).find(“ ...

  9. 进程控制之waitid函数

    Single UNIX Specification的XSI扩展包括了另一个取进程终止状态的函数--waitid,此函数类似于waitpid,但提供了更多的灵活性. #include <sys/w ...

  10. Linux基础命令(三)

    一.常用命令—文件目录类命令 1.ls 列出指定或默认目录的文件信息 使用形式: ls [选项] [目录名] 实例: $ls $ls –als $ls /home/sq/Desktop $ls ./D ...