标签:题目 str put 直接 uri cto example 记录 lse
链接:https://leetcode.com/problems/majority-element/
Level: Easy
Discription:
Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input: [3,2,3]
Output: 3
class Solution{
public:
    int arrayNesting(vector<int>& nums) {
       class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int ret[2]={0};
        for(auto n: nums)
        {
            if(n==ret[0])
                ret[1]++;
            else
            {
                if(ret[1]!=0)
                    ret[1]--;
                else
                {
                    ret[0]=n;
                    ret[1]++;
                }
            }  
        }
        return ret[0];
        
    }
};
Leetcode 169. Majority Element
标签:题目 str put 直接 uri cto example 记录 lse
原文地址:https://www.cnblogs.com/zuotongbin/p/10228301.html