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 ...
随机推荐
- Python核心编程--学习笔记--3--Python基础
本章介绍基本的Python语法.编程风格:并简要介绍标识符.变量和关键字,以及变量占用内存的分配和回收:最后给出一个较大的Python样例程序来体验这些特性. 1 语句和语法 1.1 注释 可以在一行 ...
- C# WPF使用ZXing生成二维码ImageSource
介绍: 如果需要实在WPF窗体程序中现类似如下的二维码图片生成功能,可以通过本文的方法实现 添加步骤: 1.在http://zxingnet.codeplex.com/站点上下载ZXing .Net的 ...
- 谈谈对MVC的理解
MVC是Model-View-Controler的简称,即模型-视图-控制器.其实MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开.MVC中的模型.视图.控制器它们分别承担着不同的任务 ...
- C语言中内存对齐方式
一.什么是对齐,以及为什么要对齐: 1. 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问, ...
- 行转列求和:不加 in 条件,sum的数据会不会准确?
我的习惯写法,担心不加 in 条件 ,统计结果会包含其他的数据 SELECT ZWKMYE_KJND as 年度,ZWKMYE_KJQJ as 月份,ZWKMYE_DWBH as 单位, ' then ...
- [Environment Build] Maven环境配置
1. 下载并解压maven文件 2. 在环境变量中配置一个JAVA_HOME的变量,指向你本地的JDK 3. 在系统变量中新建一个名为:MAVEN_HOME的变量,指向你的maven解压文件的bin目 ...
- hdu 4585 Shaolin
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46093 #include<algorithm> #in ...
- WordPress实现长篇文章/日志/单页面分页功能效果
在WordPress里写文章,如果内容很多,你可能想要把文章分成几页来让访客浏览,这样既保持了网页的美观,也提高了网页的打开速度.但是在WordPress默认提供的按钮里,你可能找不到文章分页功能所对 ...
- C++ MFC实现基于RFID读写器的上位机软件
C++ MFC实现基于RFID读写器的上位机软件 该博客涉及的完整工程托管在https://github.com/Wsine/UpperMonitor,觉得好请给个Star (/▽\=) 运行和测试环 ...
- Centering HTML elements larger than their parents
Centering HTML elements larger than their parents It's not a common problem, but I've run into it a ...