Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

class Solution {

public:

    //O(n) Space compexity

    vector<int> majorityElement01(vector<int>& nums) {

        vector<int> result;

        unordered_map<int, int> counts;

        int n = nums.size();

        for(auto item : nums){

            counts[item]++;

            if (counts[item] > n/ ){

               result.push_back(item); 

               counts[item] = -n; // Tricky: make sure the item only can be put into result once.

            } 

        }

        return result;

    }

    //We know, there could be at most two numbers can be more than 1/3

    //so, same as Majority Element I problem, we can have two counters.

    vector<int> majorityElement02(vector<int>& nums) {

        if(nums.size()<=) return nums;

        //the same algorithm as Majority Element I problem

        int majority1=, majority2=, cnt1=, cnt2=;

        for(auto item: nums) {

            if (cnt1 ==  && majority2 != item ) {

                majority1 = item;

                cnt1 = ;

            } else if (majority1 == item) {

                cnt1++;

            } else if (cnt2 == ) {

                majority2 = item;

                cnt2 = ;

            } else if (majority2 == item) {

                cnt2++;

            } else {

                cnt1--;

                cnt2--;

            }

        }

        //re-check it again, in case there has less than two numbers of majority

        cnt1 = cnt2 = ;

        for (auto item : nums) {

            if (majority1 == item) cnt1++;

            else if (majority2 == item) cnt2++;

        }

        vector<int> result;

        if (cnt1 > nums.size()/) result.push_back(majority1);

        if (cnt2 > nums.size()/) result.push_back(majority2);

        return result;

    }

229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数的更多相关文章

  1. 如何在O(n)的时间复杂度内找出数组中出现次数超过了一半的数

    方法一:每次取出两个不同的数,剩下的数字中重复出现次数超过一半的数字肯定,将规模缩小化.如果每次删除两个不同的数,这里当然不是真的把它们踢出数组,而是对于候选数来说,出现次数减一,对于其他数来说,循环 ...

  2. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  3. 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数

    找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...

  4. <C#>找出数组中重复次数最多的数值

    给定一个int数组,里面存在重复的数值,如何找到重复次数最多的数值呢? 这是在某社区上有人提出的问题,我想到的解决方法是分组. 1.先对数组中的所有元素进行分组,那么,重复的数值肯定会被放到一组中: ...

  5. Java实现找出数组中重复次数最多的元素以及个数

    /**数组中元素重复最多的数 * @param array * @author shaobn * @param array */ public static void getMethod_4(int[ ...

  6. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  7. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  8. 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.

    找出数组中的单身狗: 1. OddOccurrencesInArray Find value that occurs in odd number of elements. A non-empty ze ...

  9. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

随机推荐

  1. \r与\n有何区别,编码的时候应该如何使用

    \r与\n有何区别,编码的时候应该如何使用 区别: \r: 全称:carriage return (carriage是“字车”的意思,打印机上的一个部件) 简称:return 缩写:r ASCII码: ...

  2. form 登陆跳转页面练习(未连接数据库)和连接数据库版

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. XShell 安装与虚拟机连接

    XShell:是liunx的远程管理工具 为啥要用这个工具呢?因为在古老的liunx字符命令下,是看不到中文的,要么使用liunx的图形化界面(支持中文),要么使用远程管理工具,是在windows中的 ...

  4. Python标准库之Sys模块使用详解

    sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. 处理命令行参数 在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称. 使用sy ...

  5. Scrum Meeting---Seven(2015-11-2)

    今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 完成了服务器实现 5h 服务器与客户端连接测试 4h 胡亚坤 客户端与服务器端的通信 2h 客户端与 ...

  6. FlashPlayer for Android

    1. Manually install on Android devices 教程地址:“https://helpx.adobe.com/flash-player/kb/installing-flas ...

  7. [转载] zookeeper faq

    Zookeeper FAQ1. 如何处理CONNECTION_LOSS?在Zookeeper中,服务器和客户端之间维持一个长连接,CONNECTION_LOSS意味着这个连接断开了.客户端API返回C ...

  8. 展讯DTS路径及编译

    DTS路径:/kernel/arch/arm/boot/dts 如何查找修改当前TP的DTS配置(分辨率)的文件:1.查找make file,找关键字都包含CONFIG_MACH,在/kernel/a ...

  9. C# ?(问号)的三个用处(转载)

    public DateTime? StatusDateTime = null; 脑子便也出现个问号,这是什么意思呢?网上搜下,总结如下: 1. 可空类型修饰符(?): 引用类型可以使用空引用表示一个不 ...

  10. python中if __name__ == '__main__'

    python 中__name__ = '__main__' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: “Make a script both importable and execut ...