Majority Element
来源:互联网 发布:什么是多媒体数据库 编辑:程序博客网 时间:2024/03/29 17:43
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
思路:就是hashmap count,注意num.length ==1, return num[0];
public class Solution { public int majorityElement(int[] num) { if(num.length ==0) return 0; if(num.length ==1 ) return num[0]; HashMap<Integer,Integer> hashmap = new HashMap<Integer,Integer>(); for(int i=0;i<num.length; i++){ if(hashmap.containsKey(num[i])){ int count = hashmap.get(num[i])+1; if(count>num.length/2) { return num[i]; } hashmap.put(num[i],count); }else{ hashmap.put(num[i],1); } } 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
- 文件管理
- iOS tableViewCell 的移动操作 (添加长按手势)
- 天声人語 20150115
- Java设计模式——工厂设计模式
- MATLAB图像处理_图像的白平衡算法(灰色世界法)
- Majority Element
- apt-file search to search the package than include the wanted lib
- Linux 下 Qt Creator 的安装,并使用它开发你的第一个 Qt 项目全过程
- int和string相互转换
- 使用PinYin4j.jar将汉字转换为拼音
- 利用libvirt对虚拟机的网卡vnic进行QoS限速
- Android系统中标准Intent的使用
- 根类Object探究...
- alter语句