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算法

这个算法就是为解决这个问题诞生的,主要思想就是找出一对不同的元素就去掉它们,最后剩下的一定是所找的元素。

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int>& nums) {
  4. int result=nums[],count,i;
  5. for(i=,count=;i<nums.size();i++)
  6. {
  7. count+=nums[i]==result?:-;
  8. if(count>nums.size()/)
  9. break;
  10. if(count==)
  11. {
  12. count=;
  13. result=nums[i+];
  14. i++;
  15. }
  16. }
  17. return result;
  18. }
  19. }

(16ms)

2.hash

遍历数组,利用hash方式记录元素出现的次数,当某个元素次数超过⌊n/2⌋时,即为我们要找的。

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int>& nums) {
  4. unordered_map<int,int> m;
  5. int i;
  6. for(i=;i<nums.size();i++)
  7. {
  8. if(++m[nums[i]]>(nums.size()/))
  9. return nums[i];
  10. }
  11. return nums[];
  12. }
  13. };

(28ms)

3.sorting

对数组进行排序,由于要找的元素超过半数,所以下标n/2的元素一定是它,这里总数奇偶不影响,可以自己举例验证一下。

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int>& nums) { //40ms
  4. sort(nums.begin(),nums.end());
  5. return nums[nums.size()/];
  6. }
  7. };

4.randomization

随机选取数组中的一个数,验证它是不是超过半数的数字。时间最快,有一半几率选中,当然最坏的情况比较糟糕,但这不重要,就像快速排序时间复杂度一样。

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int>& nums) {
  4. srand(unsigned(time(NULL)));
  5. int tmp,i,count;
  6. while(true)
  7. {
  8. tmp=nums[rand()%nums.size()];
  9. for(i=,count=;i<nums.size();i++)
  10. {
  11. if(tmp==nums[i])
  12. count++;
  13. if(count>nums.size()/)
  14. return tmp;
  15. }
  16. }
  17. }
  18. };

(16ms)

5.bit manipulation

就是把数字都作为二进制处理,每个二进制位上的n个bit相加看是否大于n/2,大于的话这一位上是1,小于的话就是0,把32位都这么处理完就知道结果是多少了。

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int>& nums) {
  4. int i,j,count,major=;
  5. for(i=;i<;i++)
  6. {
  7. for(j=,count=;j<nums.size();j++)
  8. {
  9. if((nums[j]>>i&)==)
  10. count++;
  11. }
  12. if(count>nums.size()/)
  13. major+=(<<i);
  14. }
  15. return major;
  16. }
  17. }

(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. oracle中使用sql查询时字段为空则赋值默认

    转至:http://www.th7.cn/db/Oracle/201501/86125.shtml oracle 通过 nvl( )函数sql 查询时为 空值 赋默认值 oracle 函数介绍之nvl ...

  2. HTML的id,name,class

    HTML中的id是给JavaScript用的(document.getElementById()) HTML中的name是给JavaScript用的(formUploadFile.submit()) ...

  3. Win+R指令(2)

    1. gpedit.msc-----组策略 2. sndrec32-------录音机 3. Nslookup-------IP地址侦测器 ,是一个 监测网络中 DNS 服务器是否能正确实现域名解析的 ...

  4. jquery中链式操作的this指向

    jquery中链式操作是如何实现? 例如:$(obj).children().css('color','red').next().css('color','red').siblings().css(' ...

  5. python学习之元组

    #coding:utf-8# __author__ = 'Administrator'#元组:不可变序列 #空元组mm=()print mm#只有一个值的元组mm=(1,)print mmx=1,2, ...

  6. get和post,session和cookie的一些说明

      1.GET和POST的区别 A. 从字面意思和HTTP的规范来看,GET用于获取资源信息而POST是用来更新资源信息. B. GET提交请求的数据实体会放在URL的后面,用?来分割,参数用& ...

  7. Android_消息机制

    Android通过Looper.Handler来实现消息循环机制. Android的消息循环是针对线程的,每个线程都可以有自己的消息队列和消息循环. Android系统中的Looper负责管理线程的消 ...

  8. ubuntu彻底卸载搜狗拼音输入法

    ubuntu彻底卸载搜狗拼音输入法,ubuntu安装搜狗输入法后如果觉得搜狗不是很适合自己,那应该怎么样彻底的卸载搜狗输入法呢?下面我们就来一步步彻底卸载掉搜狗输入法... 方法/步骤 1 找到安装的 ...

  9. 【05】了解C++默默编写并调用那些函数

    1.如果没有声明copy构造方法,copy赋值操作符,和析构方法,编译器会自动生成这些方法,且是inline. 2.如果没有声明任何构造方法,编译器会自动生成一个default构造方法,且是inlin ...

  10. OpenOffice的安装与启动

    一.安装openOffice1.使用tar -xzvf OOo_3.2.0_LinuxIntel_install_wJRE_en-US.tar.gz解压缩后,会得到OOO320_m12_native_ ...