借用hashmap好理解点吧
数组中出现次数超过一半的数字
http://www.nowcoder.com/questionTerminal/e8a1b01a2df14cb2b228b30ee6a92163
int length = array.length;
HashMap<Integer,Integer> hashMap = new HashMap<>(length);
int count = 1;
int tmp = 0;
for (int i = 0; i < length; i++) {
if (hashMap.containsKey(array[i])) {
tmp = hashMap.get(array[i]);
hashMap.put(array[i], ++tmp);
if (tmp > length/2) {
return array[i];
}
}else {
hashMap.put(array[i], count);
}
}
return 0;
查看7道真题和解析