169.Majority Element

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int res = 0, cnt = 0;
        for (int num : nums) {
            if (cnt == 0) {res = num; ++cnt;}
            else (num == res) ? ++cnt : --cnt;
        }
        return res;
    }
};
posted @ 2019-04-12 09:39  JohnRed  阅读(126)  评论(0编辑  收藏  举报