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

[LeetCode]169 Majority Element

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

标签:leetcode

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

标签:leetcode

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

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