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. POJ3461–Oulipo(KMP)

    题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...

  2. 局域网接入Internet

    说在前面的话 局域网接入Internet的方式多样: 有1传统的Modem(调制解调器)拨号接入,费用低廉的2 ISDN和 3 ADSL接入,费用较高的4 DDN专线接入,5 Cable Modem高 ...

  3. fileupload控件上传、文件下载

    常遇到上传下载的功能,这里把我习惯的用法写下来: 上传: string fileName = "";fileName = this.fu_pic.FileName;if (stri ...

  4. java transient简单介绍

    我们都知道一个对象仅仅要实现了Serilizable接口,这个对象就能够被序列化,java的这样的序列化模式为开发人员提供了非常多便利,我们能够不必关系详细序列化的过程,仅仅要这个类实现了Serili ...

  5. 【Android UI设计与开发】之具体解释ActionBar的使用

    具体解释Android中的ActionBar的使用 请尊重他人的劳动成果,转载请注明出处:具体解释Android中的ActionBar的使用 http://blog.csdn.net/fengyuzh ...

  6. 【剑指offer】包括min函数的栈

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26064213 剑指offer上的第21题,之前在Cracking the Coding i ...

  7. 导入cocos2d-x samples android官方示例

    导了一晚上samples android示例,查了一晚上资料,费了很大的劲,终于成功导入并运行成功,分享一下经验: 1.下载eclipse与ADT跟android SDK,相信大家都会装了吧. 2.下 ...

  8. 动手写一个快速集成网易新闻,腾讯视频,头条首页的ScrollPageView,显示滚动视图

    最终效果 更新示例.gif 示例效果.gif 示例效果1.gif 示例效果2.gif 示例效果3.gif 示例效果4.gif 示例效果5.gif 示例效果6.gif 一.构思部分: 打算分为三个部分, ...

  9. Git使用完全解析(一)

    是时候来系统的介绍一下Git了.毫无疑问,Git是目前最优秀的分布式版本控制工具,木有之一,可是我见到的很多人还是不会用,我的老东家每天忍受着SVN带来的痛苦,却迟迟不愿切换到Git上,个人感觉,许多 ...

  10. update 改写 merge into

    update语句改写成merge into有时会提高运行速度 看两个案例 1.根据业务将两个嵌套子查询改写成max,速度有3min提升到3s UPDATE OPER_792.LL_SCB_YDKB_2 ...