Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.

Find it.

Have you met this question in a real interview? Yes

Example

Given [1, 2, 1, 2, 1, 3, 3], return 1.

Note

There is only one majority number in the array.

Challenge

O(n) time and O(1) extra space.

既然已经到1/3了不如直接用1/K的做法:

class Solution {
public:
/**
* @param nums: A list of integers
* @return: The majority number occurs more than 1/3.
*/
int majorityNumber(vector<int> nums) {
// write your code here
int stored = 2; unordered_map<int, int> counts; for (int e : nums) {
if (counts.size() < stored && counts.count(e) == 0) {
counts[e] = 1;
continue;
}
if (counts.count(e) == 0) {
auto iter = counts.begin();
while (iter != counts.end()) {
if (--iter->second == 0) {
iter = counts.erase(iter);
continue;
}
++iter;
}
} else {
counts[e]++;
}
} int maxcount = -1;
int majority = 0;
for (auto& iter : counts) {
iter.second = 0;
}
for (int e : nums) {
if (counts.count(e)) {
if (++counts[e] > maxcount) {
maxcount = counts[e];
majority = e;
}
}
} return majority;
}
};

当K=3时,由于数组中总是有数字会超过1/3个,那么我们三个一组三个一组的把(不同)数字依次除去,剩下可能又1个、2个数字的情况,当剩下数字只有一个时该数就是所求的数字,当有两个时我们是不能确定的,需要看已除去组中那个数字出现的比较多才能决定。

算法就是使用哈希来存储K-1个不同数字出现的次数,当有一个数和哈希表中任意K-1一个数都不同时,可以将这K个数作为一组整体划去(两两不同的一组数),体现为将K-1个数的计数减一(因为刚刚那个和K-1数都不同的数并没有存储到哈希表中所以不需要额外动作)。当划去过程中某个数在哈希表中计数值到达零时应该将其删去。最后再对留在哈希表中的数字做一个统计看到底那个数出现的次数多。

因而空间复杂度可以认为是O(K)的

     */
int majorityNumber(vector<int> nums, int k) {
// write your code here
int stored = k - 1;

LintCode Majority Number II / III的更多相关文章

  1. Lintcode: Majority Number II 解题报告

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

  2. Lintcode: Majority Number II

    Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...

  3. Lintcode: Majority Number III

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

  4. LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III

    LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...

  5. lintcode 中等题:Majority number II 主元素 II

    题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...

  6. Lintcode: Majority Number 解题报告

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

  7. [LintCode] Majority Number 求众数

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

  8. [LintCode] Majority Number 求大多数

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

  9. LintCode: Single Number II

    一篇解析比较详细的文章:http://www.acmerblog.com/leetcode-single-number-ii-5394.html C++ 解法(1) 求出每个比特位的数目,然后%3,如 ...

随机推荐

  1. 细说MySQL数据库操作

    目录 基本语法: 字符集和校验规则 字符集 校验规则 校验规则的影响 数据库操作相关指令 查询数据库版本 显示数据库语句 显示数据库创建语句 数据库删除语句 查看当前数据库有多少个用户在操作 基本语法 ...

  2. JDK设计模式之——责任链(Filter)

    责任链的设计模式可以参考Servlet的FilterChain.FilterChain中的每个Filter(过滤器)就像一个个的链条 web开发中 有时候需要对接口request和response进行 ...

  3. Mybatis框架四:输入参数、输出参数

    输入参数可以有三种:简单类型,POJO,包装类 关于前两种: http://www.cnblogs.com/xuyiqing/p/8600888.html 这里写一下传递包装类参数: 一个POJO:U ...

  4. Mybatis优缺点

    优点:SQL写在XML中,便于统一管理和优化 提供映射标签,支持对象和数据库的orm字段关系映射 可以对SQL进行优化      缺点: SQL工作量大 mybagtis移植姓不好 不支持级联

  5. 网页的异步请求(Ajax)

    JS原生Ajax操作(XMLHttpRequest) GET请求 var xmld=new XMLHttpRequest(); xmld.open("GET","wan. ...

  6. 函数式编程之-模式匹配(Pattern matching)

    模式匹配在F#是非常普遍的,用来对某个值进行分支匹配或流程控制. 模式匹配的基本用法 模式匹配通过match...with表达式来完成,一个完整的模式表达式长下面的样子: match [somethi ...

  7. Mysql数据库的二进制安装和基础入门操作

    前言:Mysql数据库,知识非常的多,要想学精学通这块知识,估计也要花费和学linux一样的精力和时间.小编也是只会些毛皮,给大家分享一下~ 一.MySQL安装 (1)安装方式: 1 .程序包yum安 ...

  8. 痞子衡嵌入式:蓝牙芯片厂商三强(Qualcomm&CSR, TI, Nordic)产品一览

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是蓝牙芯片三强厂商的芯片. IoT物联网是未来的趋势,半导体厂商作为IoT产业的上游,主要提供核心的无线芯片,蓝牙是比较主流的协议,下面痞 ...

  9. https://finance.sina.com.cn/realstock/company/sh600522/nc.shtml

    https://finance.sina.com.cn/realstock/company/sh600522/nc.shtml http://hq.sinajs.cn/list=sh601006

  10. 绝对路径的表示方式为什么是"/usr"而不是"//usr"

    今天闲逛贴吧,竟然看到有个人问绝对路径的表示方式为什么不是//usr/local而是/usr/local.原文: 我想99%的人都没想过这个问题,都理所当然的认为:它不就是根"/" ...