[LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half
of the size of the array. Find it.
Notice
You may assume that the array is non-empty and the majority number always exist in the array.
Given [1, 1, 1, 1, 2, 2, 2]
, return 1
O(n) time and O(1) extra space
LeetCode上的原题,请参见我之前的博客Majority Element。
解法一:
class Solution {
public:
/**
* @param nums: A list of integers
* @return: The majority number
*/
int majorityNumber(vector<int> nums) {
int res = , cnt = ;
for (int num : nums) {
if (cnt == ) {res = num; ++cnt;}
else (num == res) ? ++cnt : --cnt;
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param nums: A list of integers
* @return: The majority number
*/
int majorityNumber(vector<int> nums) {
int res = ;
for (int i = ; i < ; ++i) {
int ones = , zeros = ;
for (int num : nums) {
if ((num & ( << i)) != ) ++ones;
else ++zeros;
}
if (ones > zeros) res |= ( << i);
}
return res;
}
};
[LintCode] 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 III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- LintCode Majority Number II / III
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- Lintcode: Majority Number 解题报告
Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 169. Majority Element求众数
网址:https://leetcode.com/problems/majority-element/ 参考:https://blog.csdn.net/u014248127/article/detai ...
- 169 Majority Element 求众数 数组中出现次数超过一半的数字
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素.你可以假设数组是非空的,并且数组中的众数永远存在. 详见:https://leetcode.com/p ...
随机推荐
- 最新的jQuery插件和JavaScript库
每一个前端开发人员很清楚的重要性和功能的JavaScript库提供.它提供了一个简单的接口,用于构建快速动态的接口,而无需大量的代码. 谢谢你的超级从事jQuery开发者社区,人始终是创造新的和令人惊 ...
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
1.前言 使用框架都是较新的版本: Spring 4.0.2 RELEASE Spring MVC 4.0.2 RELEASE MyBatis 3.2.6 2.Maven引入需要的JAR包 2.1设置 ...
- Github.com的Git和TortoiseGit图文教程
图文介绍Windows系统下使用 Github账户 + msysgit + TortoiseGit 进行文件管理的方法. 安装 安装mysysgit 下载地址:msysgit 安装过程: 0.启动 1 ...
- PHP中curl的CURLOPT_POSTFIELDS参数使用细节
CURL确实是一个不错的好工具,不仅在PHP中还是其他的操作系统中,都是一个非常好用的.但是如果你有些参数没有用好的话,那可能会得不到自己理想中的结果. 在通常情况下,我们使用 CURL 来提交 PO ...
- 《征服 C 指针》摘录2:C变量的 作用域 和 生命周期(存储期)
在开发一些小程序的时候,也许我们并不在意作用域的必要性.可是,当你书写几万行,甚至几十万行的代码的时候,没有作用域肯定是不能忍受的. C 语言有如下 3 种作用域. 1.全局变量 在函数之外声明的变量 ...
- TCP/IP 和 Socket 的关系
要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...
- js简单弹出层、遮罩层
<html> <head> <title>js简单弹出层</title> <style> /*阴影边框效果*/ .box-shadow-1 ...
- spark streaming 接收 kafka 数据java代码WordCount示例
http://www.cnblogs.com/gaopeng527/p/4959633.html
- eclipse导入重复的项目、eclipse设置默认注释
- 基于分类的Vad效果
提取出频谱特征,然后使用gmm.svm.dnn模型进行训练和分类,实时话语检测. 原始文件 VAD处理,剔除静音后的效果: