给定一组数,有一个数在这组数里的出现次数超过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. jquery.validate新的写法(jquery.validate1.13.js)

    <script src="../js/jquery.js"></script> <script src="../js/jquery.vali ...

  2. python第一天作业

    作业需求 OK 开始动手了 作业要用到的知识点: python的文件操作 ####################################################3 开始了 1.先写一 ...

  3. 深入理解Java String#intern() 内存模型

    原文出处: codelog.me 大家知道,Java中string.intern()方法调用会先去字符串常量池中查找相应的字符串,如果字符串不存在,就会在字符串常量池中创建该字符串然后再返回. 字符串 ...

  4. AlertDialog.Builder对话框类的用法

    1.在测试时,如何实现一个提示 可以使用 Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); //从资源文件str ...

  5. java下的redis操作

    Java操作redis(增删改查) Java代码 package sgh.main.powersite; import java.util.ArrayList; import java.util.Ha ...

  6. 黑客群体的露面说明互联网公司开始回馈IT行业了,

    揭开中国黑客群体的神秘面纱 年薪数百万 2015-04-26 09:59:45 15259 次阅读 14 次推荐 稿源:经济观察报 33 条评论   在网络世界有专属的代号,那里才是他们最习惯的“世界 ...

  7. Windows 10 IoT Core Samples

    Windows 10 IoT Core Samples Welcome to the Windows 10 IoT Core Samples These samples have been valid ...

  8. EF - 批量插入

    比较一下下面两种方式的区别 1,每Add一次 就savechange() static void Main(string[] args) { //List<User> users= Fin ...

  9. 给Eclipse提速的7个技巧(转)

    本文由 ImportNew - 孙 波翔 翻译自 nicolasbize.欢迎加入翻译小组.转载请参见文章末尾的要求. 大约一个月前,我发表了一篇博客,其中介绍了对Eclipse的爱与恨. 有些人问我 ...

  10. js之Function原型

    在ECMAScript中,Function(函数)类型实际上是对象.每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象 ...