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. postgreSql基础命令及linux下postgreSql命令

    (1)用户实用程序: createdb 创建一个新的PostgreSQL的数据库(和SQL语句:CREATE DATABASE 相同) createuser 创建一个新的PostgreSQL的用户(和 ...

  2. CUBRID学习笔记 24 数据类型1

    ---恢复内容开始--- 一 数字类型  注意小数的四舍五入问题 1数字型 Type Bytes Mix Max Exact/approx. SHORTSMALLINT 2 -32,768 32,76 ...

  3. python strip()函数

    转发:jihite-博客园-python strip()函数 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 rm删除序列的 ...

  4. SLAM reference

    Technical website: OpenSlam: http://openslam.org/ MRPT: http://www.mrpt.org/ Monocular SLAM: https:/ ...

  5. FormsAuthentication.HashPasswordForStoringInConfigFile 方法 之研究

    摘自:http://time-is-life.cnblogs.com/articles/322523.html 给定标识哈希类型的密码和字符串,该例程产生一个适合存储在配置文件中的哈希密码. [C#] ...

  6. iOS - UIWebView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...

  7. STRUTS2 嵌套循环

    <!--begin 类目循环 --> <s:iterator value="categories" id='i' begin="0" step ...

  8. c point

    a[i] 与 *(a+i) 是等价的. 事实上在计算a[i]的值时,c语言首先将前者转换为后者形式, 而且,通常而言,用指针编写的程序要比用数组下标编写的程序执行速度快,(为什么?) 因此,应该尽量用 ...

  9. Android 呼吸灯流程分析

    一.Android呼吸灯Driver实现 1.注册驱动 代码位置:mediatek/kernel/drivers/leds/leds_drv.c 602static struct platform_d ...

  10. 在SSH框架中使用Spring的好处

    在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句.Struts是用来 ...