HashMap解法
字符串的排列
http://www.nowcoder.com/questionTerminal/fe6b651b66ae47d7acce78ffdd9a96c7
import java.util.HashMap;
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
if(array==null||array.length==0)return 0;
HashMap<Integer,Integer> m = new HashMap<Integer,Integer>();
for(int i=0;i<array.length;i++){
if(!m.containsKey(array[i]))m.put(array[i],1);
else m.put(array[i],m.get(array[i])+1);
}
//注意此处返回key
for(int key: m.keySet()){
if(m.get(key)>array.length/2)
return key;
}
return 0;
}
}
查看11道真题和解析