ltx_zero

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Num 169 

先码,回头再说,摩尔算法。。。

tle了

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int len=nums.capacity();
        int count=1;
        int now=nums[0];
        int i;
        int temp=0;
       while(1)
        {
            for(i=0;i<len;i++)
            {
            if(nums[i]==now && i!=temp)
                count++;
            else if(i!=temp)
                count--;
            if(count*2>=len) return now;
            }
           if(count>=0) return now;
           count=1;
           now=nums[++temp];
       }
        return 1;
    }
    
};
View Code

 

posted on 2019-04-23 00:30  ltx_zero  阅读(113)  评论(0编辑  收藏  举报