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

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param nums: A list of integers
  5. * @return: The majority number
  6. */
  7. int majorityNumber(vector<int> nums) {
  8. int res = , cnt = ;
  9. for (int num : nums) {
  10. if (cnt == ) {res = num; ++cnt;}
  11. else (num == res) ? ++cnt : --cnt;
  12. }
  13. return res;
  14. }
  15. };

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param nums: A list of integers
  5. * @return: The majority number
  6. */
  7. int majorityNumber(vector<int> nums) {
  8. int res = ;
  9. for (int i = ; i < ; ++i) {
  10. int ones = , zeros = ;
  11. for (int num : nums) {
  12. if ((num & ( << i)) != ) ++ones;
  13. else ++zeros;
  14. }
  15. if (ones > zeros) res |= ( << i);
  16. }
  17. return res;
  18. }
  19. };

[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. Tomcat端口被占用错误

    所报错误: 严重: Error initializing endpointjava.lang.Exception: Socket bind failed: [730013] ????????????? ...

  2. window下安装zookeeper

    本地zookeeper安装(win7)下载zookeeper-3.3.6.zip文件解压zookeeper-3.3.6.zip文件到d:盘在D:\zookeeper-3.3.6\conf下增添 zoo ...

  3. PYTHON 深拷贝,浅拷贝

    声明:本篇笔记,模仿与其它博客中的内容 浅拷贝 浅拷贝,在内存中只额外创建第一层数据 import copy n1 = {"k1": "wu", "k ...

  4. 我与A协

    大学毕业以后发现离曾经的圈子越来越远,非常怀念原来在A协和大家一起奋斗的日子,在这里写一篇文章,献给有很多美好回忆的A协,也献给渐渐远离A协的我. 首先,回顾一下我为什么会参与到A协的建设工作中来.我 ...

  5. win7无法保存打印机设置(错误0x000006d9)解决方法

    解决win7打印机共享出现‘无法保存打印机设置’操作无法完成(错误0x000006d9),接下来与大家分享下解决方法, 找到windows firewall服务,启用即可 ============== ...

  6. 【转】android中ListView的定位:使用setSelectionFromTop实现ListView的position的保持

    如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题: 解决的办法如下: 1 2 3 4 5 6 7 // 保存 ...

  7. 【XLL 框架库函数】 TempActiveRef/TempActiveRef12

    [XLL 框架库函数] TempActiveRef/TempActiveRef12 创建一个包含所有激活工作表引用区域 XLOPER/XLOPER12 LPXLOPER TempActiveRef(B ...

  8. Spring映射器、适配器、解析器

    1 springmvc的映射器和适配器 1.1springmvc的映射器 根据客户端请求的url,找到处理本次请求的handler(处理器),将url和controller关联起来 1.2spring ...

  9. (转)CDN——到底用还是不用?

    用CDN的七个理由 浏览器从服务器上下载css.js和图片等文件时都要和服务器连接,而大部分浏览器对同一个域名用于下载文件的并发连接数限制在4个,这意味着如果要下载第五个文件就必须等前四个文件中有一个 ...

  10. jmap之使用说明与JVM配置

    详情可参见:http://blog.csdn.net/fenglibing/article/details/6411953. 1 2. 3.vi 打开查看,具体介绍请看上述链接. 4.查看tomcat ...