您的位置:首页 > 其它

数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字

2016-11-16 17:27 246 查看
import java.util.TreeMap;
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
TreeMap<Integer,Integer> map = new TreeMap<Integer,Integer>();
for(int num : array){
if(true == map.containsKey(num)){
map.put(num,map.get(num)+1);
}
else{
map.put(num,1);
}
}

for(int key : map.keySet()){
if(map.get(key) > array.length / 2 ){
return key;
}
}

return 0;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐