给定一组数,有一个数在这组数里的出现次数超过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. linux之i2c子系统架构---总线驱动

    编写i2c设备驱动(从设备)一般有两种方式: 1.用户自己编写独立的从设备驱动,应用程序直接使用即可. 2.linux内核内部已经实现了一个通用的设备驱动,利用通用设备驱动编写一个应用程序(用户态驱动 ...

  2. .NET开源工作流RoadFlow-表单设计-文本框

    点击表单设计器工具栏上的文本框按钮,会弹出文本框属性对话框: 绑定字段:该文本框与表单属性设置中选择的表的某个字段绑定(该文本框中的值将会保存到该字段中). 默认值:该文本框的初始化值. 宽度:文本框 ...

  3. js的reduce方法,改变头等函数

    头等函数:把编程变成了类似搭积木的方式编码,可以使用很少的代码,实现强大的功能函数. eg: getTotal:数组的求和运算. var myArray = [1,2,3,4]; var add = ...

  4. adb 安装失败

    打开Terminal终端:Ctrl + Alt + T 按顺序执行以下三条命令:                   sudo add-apt-repository ppa:nilarrimogard ...

  5. ios学习笔记之内存管理

    一,内存管理类型定义      1,基本类型  任何C的类型,eg:      int,short,char,long,long long,struct,enum,union等属于基本类型或结构体   ...

  6. NUnit Test Adapter----单元测试需要安装这个插件

    最近通过VS2012集成Nunit的测试用例,想直接在VS中查看结果,分析测试覆盖率:所以找到了NUnit Test Adapter插件:该插件下载地址: http://visualstudiogal ...

  7. [转]ubuntu 14.04 系统设置不见了

    [转]ubuntu 14.04 系统设置不见了 http://blog.sina.com.cn/s/blog_6c9d65a10101i0i7.html 不知道删除什么了,系统设置不见了! 我在终端运 ...

  8. UISlider swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  9. objective-c自学总结(一)---面向对象

    本人大二本科在读,利用一个月多一点的时间对OC语言基础进行了自学,在下一阶段UI学习开始之前, 对这一阶段的自学进行一些总结.在此特别感谢刘晓斌学长和无线互联3G学院 首先说一下对OC的整体感觉,这是 ...

  10. Map、Set、List、Queue、Stack的特点与用法

    Collection          接口的接口   对象的集合 ├ List                   子接口      按进入先后有序保存   可重复 │├ LinkedList    ...