LeetCode OJ 之 Majority Element II(大部分元素2)

原创 2015年07月09日 08:25:53

题目:

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.

给一个含有n个整数的数组,找出所有出现次数超过 n/3 次的元素,时间复杂度O(n) ,空间复杂度O(1).

思路:

结果最多出现两个数字。思路类似http://blog.csdn.net/u012243115/article/details/42076523.

代码:

class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) 
    {
        int times1 = 0 , times2 = 0;
        int num1 = 0 , num2 = 0;
        int len = nums.size();
        vector<int> result;
        for(int i = 0 ; i < len ; i++)
        {
            if(times1 == 0 || nums[i] == num1)
            {
                times1++;
                num1 = nums[i] ;
            }
            else
                if(times2 == 0 || nums[i] == num2)
                {
                    times2++;
                    num2 = nums[i];
                }
                else
                {
                    times1--;
                    times2--;
                }
        }
        times1 = 0 , times2 = 0;
        for(int j = 0 ; j < len ; j++)
        {
            if(nums[j] == num1)
                times1++;            
            if(num1 != num2 && nums[j] == num2)
                times2++;
        }
        if(times1 > len/3)
            result.push_back(num1);
        if(times2 > len/3)
            result.push_back(num2);
        return result;
    }
};


版权声明:转载请注明出处。

相关文章推荐

LeetCode-169.229. Majority Element II (JAVA)主要元素

LeetCode-169.229. Majority Element II (JAVA)主要元素

LeetCode 229 Majority Element II(主要元素II)(Array)(Boyer–Moore majority vote algorithm)

原文给定一个长度为n的整型数组,找出所有出现超过 ⌊ n/3 ⌋ 次的元素。算法应该运行在线性时间上,且进用O(1)O(1)空间。提示:它可能有多少个主要元素?原文Given an integer a...
  • NoMasp
  • NoMasp
  • 2016年08月29日 21:40
  • 1415

【LeetCode】229.Majority Element II解题报告

【LeetCode】229.Majority Element II解题报告tags: Array 题目地址:https://leetcode.com/problems/majority-element...

[leetcode] 229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algori...

[leetcode] 229. Majority Element II 解题报告

题目链接:https://leetcode.com/problems/majority-element-ii/ Given an integer array of size n, find all...

Leetcode——229. Majority Element II

题目Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algor...

[LeetCode] Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊...

LeetCode-Majority Element II

原题链接:https://leetcode.com/problems/majority-element-ii/ 题目描述:题目要求找出一个数是在一个数组中出现大于n/3次的。 思路:这道题要求用O...
  • leeteer
  • leeteer
  • 2015年09月15日 05:25
  • 161

[Leetcode] 229. Majority Element II 解题报告

题目: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Th...

leetcode笔记--Majority Element I & II

 Majority Element 题目:难度(Easy) Given an array of size n, find the majority element. The majority...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode OJ 之 Majority Element II(大部分元素2)
举报原因:
原因补充:

(最多只允许输入30个字)