Java-Majority Element
来源:互联网 发布:条顿森林知乎 编辑:程序博客网 时间:2024/04/28 08:02
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.
要求找出出现次数过半的数组元素 只要使用哈希表 进行一次遍历存储 满足hm.get(num[i])>num.length/2-1的即使所求结果 代码如下:
public class Solution { public int majorityElement(int[] num) { Map<Integer,Integer> hm=new HashMap<Integer,Integer>(); for(int i=0;i<num.length;i++){ if(hm.containsKey(num[i])){ if(hm.get(num[i])>num.length/2-1){ return num[i]; }else{ hm.put(num[i],hm.get(num[i])+1); } }else{ hm.put(num[i],1); } } return num[0]; }}
0 0
- Majority Element (Java)
- Java-Majority Element
- 【leetcode Java】Majority Element
- [LeetCode][Java] Majority Element
- LeetCode|Majority Element-java
- (java)Majority Element
- LeetCode Majority Element JAVA
- LeetCode169. Majority Element(JAVA)
- LeetCode(169) Majority Element (Java)
- [leetcode-169]Majority Element(java)
- LeetCode 169 -Majority Element ( JAVA )
- leetcode-java-169. Majority Element
- LeetCode-169. Majority Element (Java)
- 169. Majority Element(java)待续。。。
- Majority Element 和 Majority Element II LeetCode Java
- majority element
- Majority Element
- Majority Element
- join的几种实现
- boost log 写不进本地的日志文件
- cocos2dx 3.0 quick lua transition action
- 安卓中实现对SQLITE数据表的增删改查
- windows下配置Sublime Text 2开发Nodejs
- Java-Majority Element
- Android动画之translate(位移动画)
- 项目系统中的YUV数据显示 图简单用了Opencv 但要转换成IplImage数据
- 管理Fragmet
- ListView 详解(适合初学者)
- 跨平台移动开发phonegap/cordova 3.3全系列教程-helloworld
- Android ImageView图片自适应
- LeetCode 5:《Longest Palindromic Substring》
- Centos服务器下LAMP+wordpress安装过程