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. 18年最有"钱"途的专业就是它(文末有福利)

    根据社会调查机构麦可思发布的<2018年中国大学生就业报告>中得知,从就业率.薪资和就业满意度等多角度综合考量,信息安全专业为首推绿牌专业. 不管你是计算机相关专业的学生,还是已经工作的I ...

  2. 【ISC安全训练营】挑战价格极限第三天!!![北京]

    每到周三都觉得离周末不远啦,人生都充满的了希望,同样的,今天的优惠福利依旧超级给力,错过了可就没有了哦! 周三福利 名额 周四福利 名额 周五福利 名额 3折购买任意课程资格 3名 4折购买任意课程资 ...

  3. 微服务中Feign快速搭建

    在微服务架构搭建声明性REST客户端[feign].Feign是一个声明式的Web服务客户端.这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释.它具有可插入注释支持,包 ...

  4. node.js服务器搭建

    //1.导入http 核心模块 const http = require("http"); //2.调用http.createServer 方法,创建一个web 服务器对象 con ...

  5. JSON库的使用研究(二)

    Java 中哪个 JSON 库的解析速度是最快的? 这个问题有意义吗?各个JSON库的性能差距不大?呵呵,差距大不大,自己往下看吧! 这个问题我们应该分为以下四个维度进行研究: 1.序列化 2.反序列 ...

  6. 破解第二课 JMP法

    首先,我用录屏大师自制了一个视频,给视频加上密码.任意输入,看到报错信息“密码不对,请重新输入” 第一步 反汇编窗口右键点击“中文搜索引擎”---“智能搜索”,搜索引擎界面再次搜索“不对”,结果如下: ...

  7. 避免jquery的click多次绑定方法

    $("#xxx").click(function(){}) 这样只是会在原click方法中继续添加新方法: 当然解决办法是解绑: $("#xxx").unbin ...

  8. 2014--My Plan

    写于2014/1/10 从2014年开始我每年规划自己的life,每年10个plans. 回忆2013: 2013年,改变了很多.准确的说,那10个月,像个漫长的旅程,像个人生的转折点,应该可以这么说 ...

  9. 关于tensorflow conv2d卷积备忘的一点理解

    **************input************** [[[[-0.36166722  0.04847232  1.20818889 -0.1794038  -0.53244466] [ ...

  10. linux 两个查找工具 locate,find

    linux 中有很多查找工具,今天主要讲解locate,find两个工具. 一.locate 1.性能介绍 查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db 注意 ...