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.

Have you met this question in a real interview?

 
 
Example

Given [1, 1, 1, 1, 2, 2, 2], return 1

Challenge

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 求大多数的更多相关文章

  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 III

    Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...

  3. 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 ...

  4. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

  5. Lintcode: Majority Number 解题报告

    Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...

  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. 【Lintcode】046.Majority Number

    题目: Given an array of integers, the majority number is the number that occurs more than half of the ...

随机推荐

  1. wxParse解析富文本内容使点击图片可以选中并实现放大缩小

    wxParse解析富文本内容不多说,之前写过步骤介绍,主要是在使用过程中发现解析的富文本内容里有图片时有的可以点击放大缩小,有的点击却报错,找不到imgUrls. 经过排查发现:循环解析的富文本内容正 ...

  2. sql server management studio 查询的临时文件路径

    C:\Users\你的登录名称\Documents\SQL Server Management Studio\Backup Files C:\Users\你的登录名称\AppData\Local\Te ...

  3. 使用lightProbe来模拟动态物体的照明shader

    VertexLit path中读取lightProbe烘焙信息: Shader "VertexLitProbe" { Properties { _MainTex ("Ba ...

  4. 开源的.NET系统推荐

    C# 源码 AForge.NET     RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的 ...

  5. STC单片机 IAP(EEPROM)的使用

    STC89C51.52内部都自带有2K字节的EEPROM,54.55和58都自带有16K字节的EEPROM,STC单片机是利用IAP技术实现的EEPROM,内部Flash擦写次数可达100,000 次 ...

  6. 视觉SLAM中的数学基础 第三篇 李群与李代数

    视觉SLAM中的数学基础 第三篇 李群与李代数 前言 在SLAM中,除了表达3D旋转与位移之外,我们还要对它们进行估计,因为SLAM整个过程就是在不断地估计机器人的位姿与地图.为了做这件事,需要对变换 ...

  7. grep与孪生兄弟egrep差异

    egrep是对grep的功能扩展,让其支持正则更加完美! #grep与egrep不同  egrep完全支持正则 ls |grep -i '[a-z]\{3\}'    === ls |egrep -i ...

  8. 基于Centos搭建 Mono 开发环境

    系统要求: CentOS 7.2 64 位操作系统 安装 Mono 安装前的准备 yum install yum-utils 执行命令添加安装包仓库 rpm --import "http:/ ...

  9. kubernetes中port、target port、node port的对比分析,以及kube-proxy代理

    转:http://blog.csdn.net/xinghun_4/article/details/50492041 容器网络实例 服务中的3个端口设置 这几个port的概念很容易混淆,比如创建如下se ...

  10. cmake 常用变量和常用环境变量查表手册---整理 .

    一,cmake 变量引用的方式: 前面我们已经提到了,使用${}进行变量的引用.在 IF 等语句中,是直接使用变量名而不通过${}取值 二,cmake 自定义变量的方式: 主要有隐式定义和显式定义两种 ...