Majority Element
来源:互联网 发布:耽美小说完结推荐知乎 编辑:程序博客网 时间:2024/03/28 21:57
Majority Element
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
public class Solution { public int majorityElement(int[] num) { Map map = new HashMap<Integer, Integer>();int tmp = 0;for (int i = 0; i < num.length; i++) {if (!map.containsKey(num[i])) {map.put(num[i], 1);} else {tmp = (Integer) map.get(num[i]);tmp = tmp + 1;map.put(num[i], tmp);}}tmp = 0;int val = 0;int key = 0;Iterator ite = map.entrySet().iterator();while (ite.hasNext()) {Map.Entry entry = (Map.Entry) ite.next();val = (Integer) entry.getValue();if (val > tmp) {tmp = val;key = (Integer) entry.getKey();}}return key; }}
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
- 每个程序员都应该知道的福利
- API
- Hibernate @OneToOne -- 1
- select隐藏选中值对应的id,显示其它id
- 关于ios证书
- Majority Element
- jQuery 隔行换色
- android开机动画启动流程
- DEDECMS列表页随机调用多张缩略图
- java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
- Ceph命令集合
- 集合框架
- C#+OpenGL编程之再见小桃子(The Tao Framework)
- UVA138(数论问题二分打表)