Majority Element
来源:互联网 发布:双十一淘宝海报尺寸 编辑:程序博客网 时间:2024/06/05 10:15
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.
public class Solution { public int majorityElement(int[] num) { int len = num.length;HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();for(int i=0;i<len;++i){if(!map.containsKey(num[i])){map.put(num[i], 1);if(len == 1)return num[i];}else {map.put(num[i], map.get(num[i])+1);if(map.get(num[i]) > len/2)return num[i];}}for(Map.Entry<Integer, Integer> entry:map.entrySet()){int key = entry.getKey();int count = entry.getValue();if(count >=len/2)return key;}return -1; }}
0 0
- majority element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- Majority Element
- majority element
- Majority Element
- Majority Element
- PHP计划任务:如何使用Linux的Crontab执行PHP脚本
- 2015年3-4月 同事面试题目汇总
- 存储过程的优缺点
- Apache2不能启动的解决办法(提示could not bind to address 0.0.0.0:80)
- 时间复杂度和空间复杂度 3
- Majority Element
- 垂直居中的几种方法比较(纯CSS)
- 搜索背后的奥秘——浅谈语义主题计算
- std::function与std::bind 函数指针
- 多线程编程复习 windows线程函数必须为全局函数或者静态函数
- Exec函数对应的系统调用处理过程
- CS 106A JAVA学习
- 滚粗的HNOI2015
- 第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛