leetcode---Majority Element


//利用寻找发帖水王解决方法
public class Solution {
    public int majorityElement(int[] nums) {
        int cadinate = 0;
        int ntimes = 0;
        for(int i = 0; i < nums.length; i++){
            if(ntimes == 0){
                cadinate = nums[i];
                ntimes++;
            }else{
                if(nums[i] == cadinate){
                    ntimes++;
                }else{
                    ntimes--;
                }
            }
        }
        return cadinate;
    }
}


你可能感兴趣的:(leetcode---Majority Element)