sharing my easy cpp solution~


  • 0
    D

    class Solution {
    public:
    vector<int> majorityElement(vector<int>& nums)
    {
    sort(nums.begin(),nums.end());
    int j=0,k=nums.size()/3;
    vector<int> res;
    int status=-1;
    for(;k<nums.size();)
    {
    if(nums[j]==nums[k])
    if(status==-1)
    res.push_back(nums[j]),status++;
    else if(nums[j]!=res[status])
    res.push_back(nums[j]),status++;
    j++;
    k++;

        }
        return res;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.