leetcode majority number
给定一组数,有一个数在这组数里的出现次数超过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的更多相关文章
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- [LintCode] Majority Number 求大多数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Majority Number I & || && |||
Majority Number Given an array of integers, the majority number is the number that occurs more than ...
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- Leetcode: Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- 包装类(Wrapper Class)
1)包装类.针对于原生数据类型的包装.所有的包装类(8个)对位于java.lang包下.java中的8个包装类分别是:Byte,Short,Integer,Long,Float.Double,Char ...
- StyleCop学习笔记——初识StyleCop
一.定义 StyleCop是微软的一个开源的静态代码分析工具,检查c#代码一致性和编码风格. 二.支持的环境. JetBrains R# 5.1.3 ( 5.1.3000.12) JetBrains ...
- SpringMvc中Interceptor拦截器用法
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆等. 一. 使用场景 1 ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...
- hdu 1113 Word Amalgamation
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 字符串简单题: stl水过 如下: #include<algorithm> #inc ...
- WebClient和HttpReuqest两种网络请求的方式
相对来说webClient请求的方式比较简单,可以直接通过new的方式创建一个实例,然后调用OpenReadAsync方法传进一个url,最后通过回调函数OpenReadCompleted就可以获取网 ...
- iOS学习之C语言分支结构
一.BOOL类型 返回值:真:YES 假:NO 定义一个布尔类型的变量 YES == 1, NO == 0 计算机在识别时,YES就替换成1,NO就替换成0 BOOL isGirl = YES; ...
- 开启Objective-C --- OC基础知识
一.Objective-C简述 Objective-C通常写作ObjC和较少用的Objective C或Obj-C,是扩充C的面向对象编程语言.Objective-C主要用于:编写iOS操作 ...
- 28.USB的传输类型
USB上必须将数据组织成 事务 才能够进行传输.事务常有两个或三个包.令牌包用于启动一个事务,由主机发送:数据包传送数据,方向由令牌包确定:握手包常是数据接收方发送的,用于表示接收数据的状态.USB协 ...
- Labview实现频率调制(FM)
Labview实现频率调制(FM) 频率调制的原理: 自己的实现为三角函数分解 根据这个公式在Labview中连线则可以得到最终的波形输出 实现效果 从频域图中可以看出,载波信号的频率被调制,原本为双 ...