15算法课程 169. Majority Element

来源:互联网 发布:港融数据大平台主页 编辑:程序博客网 时间:2024/06/15 21:24


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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.


solution:

由于一定存在主要的元素且数量大于n/2,所有可以使用计数抵消的方法,选第一个元素为主要元素,
如果下一个元素与之相同则计数加一,否则计数减一;当计数值为0时,在选择下一个元素为主要元素,
重复上述过程,最后主要元素的值即使所求。


code:

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



原创粉丝点击