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. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 169. Majority Element求众数

    网址:https://leetcode.com/problems/majority-element/ 参考:https://blog.csdn.net/u014248127/article/detai ...

  9. 169 Majority Element 求众数 数组中出现次数超过一半的数字

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素.你可以假设数组是非空的,并且数组中的众数永远存在. 详见:https://leetcode.com/p ...

随机推荐

  1. 关于war包 jar包 ear包 及打包方法

    关于war包 jar包 ear包 及打包方法 war包:是做好一个web应用后,通常是网站打成包部署到容器中 jar包:通常是开发的时候要引用的通用类,打成包便于存放管理. ear包:企业级应用 通常 ...

  2. mysql 存储过程在批处理数据中的应用

    最近批处理数据的时候,突然想到:为什么不使用存储过程进行数据批处理? 为什么要进行批处理? 自答:减少数据库连接次数,提高效率. 存储过程批处理数据的优点:一次编译,永久执行. 这次的批处理逻辑较简单 ...

  3. Chrome 及其 插件“个性化设置”备份

    Chrome版本发布时间表 2016.10.13 v54.0.2840.59  主题颜色由 蓝色 变为 灰色 2016.11.17 重新使用 Chrome 浏览器(v54.0.2840.99),并设置 ...

  4. php中几个字符串替换函数详解

    在php中字符替换函数有几个如有:str_replace.substr_replace.preg_replace.preg_split.str_split等函数,下面我来给大家总结介绍介绍. 一.st ...

  5. Hydra用户手册

    Hydra 参数: -R继续从上一次进度接着破解 -S大写,采用SSL链接 -s <PORT>小写,可通过这个参数指定非默认端口 -l <LOGIN>指定破解的用户,对特定用户 ...

  6. Python自动化之一对多

    一对多 建立一对多关系之后(也就是加了外键),会在字表里面多一个"外键字段_id"这个字段 查询 #_*_coding:utf-8_*_ from django.db import ...

  7. hive的数据导出方式

    hive有三种导出数据的方式 >导出数据到本地 >导出数据到hdfs >导出数据到另一个表   导出数据到本地文件系统 insert overwrite local director ...

  8. Android开发之---Activity生命周期

    Android开发中,有四大组件:Activity.Service.Content Provider.Broadcast Receiver,可以说,activity的使用是最频繁的了,这里来梳理一下与 ...

  9. Spring事务管理

    Spring是SSH中的管理员,负责管理其它框架,协调各个部分的工作.今天一起学习一下Spring的事务管理.Spring的事务管理分为声明式跟编程式.声明式就是在Spring的配置文件中进行相关配置 ...

  10. Oracle insert大量数据经验之谈(转)

    在很多时候,我们会需要对一个表进行插入大量的数据,并且希望在尽可能短的时间内完成该工作,这里,和大家分享下我平时在做大量数据insert的一些经验. 前提:在做insert数据之前,如果是非生产环境, ...