美文网首页
229. Majority Element II

229. Majority Element II

作者: 默写年华Antifragile | 来源:发表于2020-03-29 20:50 被阅读0次

https://leetcode.com/problems/majority-element-ii/

给定一个数组,其长度为 n, 找出其中出现次数超过 n/3 的数,可能有一个,也可能有两个
Note: The algorithm should run in linear time and in O(1) space.
----------------------------------------------------------------------------------------------------------
Example 1:
Input: [3,2,3]
Output: [3]
---------------------
Example 2:
Input: [1,1,1,3,3,2,2,2]
Output: [1,2]

分析:与 https://www.jianshu.com/p/adab40f7a25b 类似,可以从数组中同时减去3个不同的数字

class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
        int ntms1=0, ntms2=0;
        int temp1=0, temp2=0;
        for(int i=0; i<nums.size(); ++i)
        {
            if(ntms1==0 && nums[i]!=temp2) //这里注意要加一个判断 nums[i]不等于temp2, 不加 [1,2,2,3,2,1,1,3]出错
            {
                temp1=nums[i];
                ntms1=1;
            }
            else if(temp1==nums[i])
            {
                ntms1++;
            }
            else if (ntms2==0)
            {
                temp2 = nums[i];
                ntms2 = 1;
            }
            else if (temp2==nums[i])
            {
                ntms2 ++;
            }
            else
            {
                ntms1--;
                ntms2--;
            }
        }

        ntms1=0,ntms2=0;
        for(int i=0; i<nums.size();++i)
        { 
            if(nums[i]==temp1)
                ntms1++;
            else if(nums[i]==temp2)
                ntms2++;
        }

        int k = (nums.size()/3);
        vector<int> result;
        if(ntms1>k)
            result.push_back(temp1);
        if(ntms2>k)
            result.push_back(temp2);
        return result;
    }
};

结果:

Runtime: 12 ms, faster than 88.67% of C++ online submissions for Majority Element II.
Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Majority Element II.

相关文章

  • II Boyer-Moore Majority Vote Alg

    229. Majority Element II Given an integer array of size n...

  • 229. Majority Element II

    先排序 然后顺序扫描,需要注意边缘,代码如下: 但是这道题目限制了时间是限行时间空间是o(1),因此,既不能使用排...

  • 229. Majority Element II

    Given an integer array of size n, find all elements that ...

  • 229. Majority Element II

    题目 题目很简短,给定一个包含n个数字的数组,找出所有出现次数超过 ⌊ n/3 ⌋ 次的数。要求O(n)时间复杂度...

  • 229. Majority Element II

    思路就是三个三个删,如何描述是三个三个删的关键是两个count要描述好。之前没仔细写,count应该有四种case...

  • 229. Majority Element II

    Question Given an integer array of size n, find all eleme...

  • 229. Majority Element II

    https://leetcode.com/problems/majority-element-ii/ 给定一个数组...

  • 229. Majority Element II

    给定一个大小为 n 的整数数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 摩尔投票法 大于 n/3 的数最...

  • 229. Majority Element II (M)

    Given an integer array of size n, find all elements that ...

  • Majority Element II

    Majority Element II 今天是一到有关数学计算的题目,是Majority Element(回复01...

网友评论

      本文标题:229. Majority Element II

      本文链接:https://www.haomeiwen.com/subject/hgxpuhtx.html