C++ ,use stl nth_element


  • 0
    class Solution {
    public:
    int majorityElement(vector<int>& nums) {
        int size=nums.size();
        nth_element(nums.begin(),nums.begin()+size/2,nums.end());
        return nums[size/2];
    }
    };

Log in to reply
 

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