Show my c++ code(30ms)


  • 0
    C
    class Solution {
    public:
        int majorityElement(vector<int> &num) {
            int s_num=num.size();
            if(s_num==1) return num[0];
            int i=0;
            while(i<s_num/2+1){
                int count=1;
                for(int j=i+1;j<s_num;j++){
                    if(num[i]==num[j]){
                        count++;
                        if(count>=(s_num+1)/2)
                           return num[j];
                    }
                }
                i++;
                while(num[i-1]==num[i])
                    i++;
            }            
        }
    };

Log in to reply
 

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