在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素。
容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element。
但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):
1、Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相同则count++,不同则count--,当count=0则选择数组中的下一个数作为candidate,遍历结束candidate则为majority element。

public int majorityElement(int[] num) {
int count = 1;
int candidate = num[0];
for (int i = 1; i < num.length; i++) {
if (count == 0) {
candidate = num[i];
}
if (num[i] == candidate) {
count++;
} else {
count--;
}
}
return candidate;
}

2、这种方式挺有意思。因为majority element出现的次数大于 ⌊ n/2 ⌋ 次,而题目中给的是int类型数组,对于每一个数字的32位,majority element的每一位是相同的,所以不管这一位是1或0,出现次数多的总是majority element的。

下面之所以用C++的方式来实现,是因为在用Java来写的过程中出现了个问题就是Math.pow(a,b)来计算幂次的时候结果是double类型,强转之后丢失一半,所以还要处理这个情况比较麻烦,用C++来没有出现这个问题。

class Solution
{
public:
int majorityElement(vector<int> &num) {
int bitCnt[];
memset(bitCnt, , sizeof(bitCnt)); for (int i = ; i < num.size(); i++) {
for (int j = ; j < ; j++) {
if (num[i] & ( << j))
bitCnt[j]++;
}
} int ans = ;
for (int i = ; i < ; i++) {
if (bitCnt[i] > num.size()/)
ans += (int)pow(, i);
}
return ans;
}
};

LeetCode——Majority Element的更多相关文章

  1. 2016.5.18——leetcode:Majority Element

    Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...

  2. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. [LeetCode] Majority Element 求众数

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

  4. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

  5. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  6. [LeetCode] Majority Element 求大多数

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

  7. LeetCode Majority Element I

    原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...

  8. LeetCode Majority Element Python

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

  9. Leetcode: Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

随机推荐

  1. Qt 学习之路 :Qt 线程相关类

    希望上一章有关事件循环的内容还没有把你绕晕.本章将重新回到有关线程的相关内容上面来.在前面的章节我们了解了有关QThread类的简单使用.不过,Qt 提供的有关线程的类可不那么简单,否则的话我们也没必 ...

  2. C#读取Word文档内容代码

    首先要添加引用com组件:然后引用: using Word = Microsoft.Office.Interop.Word; 获取内容: /// /// 读取 word文档 返回内容 /// //// ...

  3. NSString截取文件名(很笨的方法)

    对NSString的操作不熟悉,目前采用以下方法取得路径中的文件名以及文件夹路径 NSString* test=[[NSString alloc]initWithFormat:@"D:\\c ...

  4. js关于闭包的内存的问题--deep down

    js有一个东西叫做GC(garbage collection )垃圾回收机制;js中有两种类型:js基本数据类型,js引用类型; 当一个函数[对象]--引用类型被引用后,过后,出了它的功能之后,gc会 ...

  5. C#中的线程二(BeginInvoke和Invoke)

    近日,被Control的Invoke和BeginInvoke搞的头大,就查了些相关的资料,整理如下.感谢这篇文章对我的理解Invoke和BeginInvoke的真正含义 . (一)Control的In ...

  6. datazen logo修改

    第一步:进入cp 页面 第二步: P161说明文档的P161开始,这里有说有版本的图片命名 第三步:再次强调第二步的命名,否则 如果你直接复制这个名字,不但效果出不来,还删除不掉这个包,我上次是把da ...

  7. emmt html生成

    html:5  或 ! html:5 或!:用于HTML5文档类型 html:xt:用于XHTML过渡文档类型 html:4s:用于HTML4严格文档类型 常用过渡文档类型  html:xt  直接c ...

  8. 优秀的弹窗插件 jquery.lightbox_me.js

    项目地址: https://github.com/buckwilson/Lightbox_me用法:http://buckwilson.me/lightboxme/ var opt = { 'cent ...

  9. 如何做好Flex与Java交互

    三种flex4与Java顺利通信的方式是: flex与普通java类通信RemoteObject; flex与服务器交互HTTPService; flex与webservice交互WebService ...

  10. underscorejs-map学习

    2.2 map 2.2.1 语法: _.map(list, iteratee, [context]) 2.2.2 说明: 对集合的每个成员依次进行某种操作,将返回的值依次存入一个新的数组.接收3个参数 ...