给定一组数,有一个数在这组数里的出现次数超过n/2次。

求出这是哪个数

https://leetcode.com/problems/majority-element/

一开始考虑的方是将所有数转化为二进制,那么对于这些二进制数来说。

其每一位上必然是出现次数最多的数决定了它是1还是0。

例如,将每个二进制数的最低位加起来,得到一个sum。

那么如果sum/nums.length大于0.5那么这个majority number在最低位必然是1

反之必然为0。这样就可以把所有的位置求出来。AC了

但是网上查到了一个更好的解法。

即,每出现一对不同的数,就删除它,那么到最后剩下的那个数必然是所求的数。

网上给出的代码也极为精巧

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

leetcode majority number的更多相关文章

  1. [LintCode] Majority Number 求众数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  2. [LintCode] Majority Number 求大多数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  3. Majority Number I & || && |||

    Majority Number Given an array of integers, the majority number is the number that occurs more than ...

  4. Lintcode: Majority Number III

    Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...

  5. Leetcode: Majority Element II

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

  6. Lintcode: Majority Number II

    Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...

  7. lintcode 中等题:majority number III主元素III

    题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...

  8. lintcode 中等题:Majority number II 主元素 II

    题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...

  9. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. 将python2.7+django1.10部署到SAE上

    首先我想说的是我为什么选择SAE呢?本人学生一枚,没钱.然后sae好像又有免费的一定限额,所以我就选了它. 期间曲折颇多,实在不是三言两语所能道情的.各种百度,谷歌,最后所幸成功了,幸哉! 主要参考了 ...

  2. Linux下如何查看JDK安装路径

    1:echo $JAVA_HOME 使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$JAVA_HOME,否则如下所示,根本定位不到JDK的安装路径 [root@localho ...

  3. kettle日志记录

    环境描述: 现在一个项目有很多个作业,需要知道每次跑批后哪些ktr跑成功,哪些失败了 问题解决: 下面是一个具体的操作流程 首先建立数据库表 CREATE TABLE test_1(id INT,NA ...

  4. Android操作系统11种传感器介绍

    我们依次看看这十一种传感器 1 加速度传感器 加速度传感器又叫G-sensor,返回x.y.z三轴的加速度数值. 该数值包含地心引力的影响,单位是m/s^2. 将手机平放在桌面上,x轴默认为0,y轴默 ...

  5. MVC 初始 Log4net (一)

    以前没有使用过Log4net 插件来记录日志文件,今天研究了一下,算是有点小眉目了,只是简单的使用一下:来写一篇博客自己记录一下,希望大神们多多包涵,小伙伴多多给提些建议,相互学习,我也是初始阶段,有 ...

  6. [开源应用]利用HTTPHandler+resumableJs+HTML5实现拖拽上传[大]文件

    前言: 大文件传输一直是技术上的一大难点.文件过大时,一些性提交所有的内容进内存是不现实的.大文件带来问题还有是否支持断点传输和多文件同时传输. 本文以resumableJs为例,介绍了如何在ASP. ...

  7. Python实现ID3(信息增益)

    Python实现ID3(信息增益) 运行环境 Pyhton3 treePlotter模块(画图所需,不画图可不必) matplotlib(如果使用上面的模块必须) 计算过程 st=>start: ...

  8. U盘安装 Windows XP 原版 ISO 的几点心得

    虽然我一直致力于推动最新操作系统的部署,劝说周围朋友尽快淘汰 Windows XP,但还是难免有一些老电脑.老朋友的电脑,坚持要使用 XP 系统. 这里有几点使用U盘安装 Windows XP 原版 ...

  9. kibana 修改Ico图标

    修改此路径下的E:\happy\kinbana\kibana-4.2.2-windows\kibana-4.2.2-windows\optimize\bundles的commons.bundle.js ...

  10. SPOJ - DQUERY 主席树

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32356 Given a sequence of n numbers ...