leetcode-Majority Element
2015-11-04 22:05
218 查看
用哈希表保存出现的次数即可。
public class Solution { public int majorityElement(int[] nums) { int max=0,result=0,curLen=0; HashMap<Integer,Integer> ha=new HashMap<Integer,Integer>(); for(int i=0;i<nums.length;i++) { if(!ha.containsKey(nums[i])) ha.put(nums[i], 1); else { curLen=ha.get(nums[i]); curLen++; ha.put(nums[i], curLen); } curLen=ha.get(nums[i]); if(curLen>max) { max=curLen; result=nums[i]; } } return result; } }
相关文章推荐
- JDK 安装环境配置(ubuntu)
- [leetcode 112]Path Sum
- JDK 安装环境配置(ubuntu)
- 《生活就是一个七天接着一个七天》
- 多个git账户生成多份rsa秘钥实现多个账户同时使用配置
- Mac androidstudio 快捷键
- Shader 学习笔记 20151104
- B站小爬虫
- java基础之求质数练习
- [CXF] Server与Client实现方式四:JMS
- UVA 11478(差分约束 + 二分)
- 1017. Queueing at Bank (25)
- Linux下无效唤醒的应用
- 1017. Queueing at Bank (25)
- c语言中的输入
- Delete Node in a Linked List
- iOS 画板的简单实现
- SqlServer try catch 捕获不到的一些错误及解决方法
- JavaScript中声明全局变量的三种方式(包括显示和隐式)
- HDU1257