码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Majority Element"

时间:2014-12-23 06:43:36      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

Really interesting O(n) problem. Just like playing.. so actually working on algorithm problems is like playing games!

class Solution {
public:
    int majorityElement(vector<int> &num) 
    {
        int ret = num[0];
        int cnt = 1;
        for (int i = 1; i < num.size(); i ++)
        {
            if (cnt == 0) ret = num[i];
            if(num[i] == ret)    cnt ++;
            else                cnt--;
        }
        return ret;
    }
};

LeetCode "Majority Element"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4179352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!