首页 > 其他 > 详细

[LeetCode]169 Majority Element

时间:2015-01-09 19:29:36      阅读:259      评论:0      收藏:0      [点我收藏+]

https://oj.leetcode.com/problems/majority-element/

public class Solution {
    public int majorityElement(int[] num) {
        
        int len = num.length;
        int minhit = (len / 2) + 1;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : num)
        {
            Integer occur = map.get(i);
            if (occur == null)
                occur = 0;
            occur ++;
            
            if (occur >= minhit)
                return i;
            else
                map.put(i, occur);
        }
        // throw exception
        throw new IllegalStateException("input is wrong");
    }
}


[LeetCode]169 Majority Element

原文:http://7371901.blog.51cto.com/7361901/1601314

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!