C++,use map to solve


  • 0
    B

    class Solution {
    public:
    int majorityElement(vector<int>& nums) {
    map<int,int> mapTemp;
    int leng = nums.size();
    int temp ;
    for(int i =0;i<leng;i++)
    {
    mapTemp[nums.at(i)]++;
    }
    for(auto c: mapTemp)
    {
    if((c.second)*2 >= leng)
    {
    temp = c.first;
    break;
    }
    else temp = -1;
    }

                return temp;
        
    }
    

    };


Log in to reply
 

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