Majority Element

来源:互联网 发布:eclipse导入jar包源码 编辑:程序博客网 时间:2024/06/05 02:22

感觉是真的困了啊,哈哈哈哈,啦啦啦啦,红红火火恍恍惚惚哼哼哈嘿

public class Solution {    public int majorityElement(int[] nums) {        int t = nums.length / 2;        Map<Integer/*element*/, Integer/*times*/> map = new HashMap<>();        int s = 0;        for (int e: nums) {            ///// 2            s = e;            ///// 2            if (map.containsKey(e)) {                //3 int times = map.get(e);                int times = map.get(e) + 1;                if (times > t) {                    return e;                }                map.put(e, times);            } else {                map.put(e, 1);            }        }        //1 return -1;        return s;    }}


0 0