Sword to Offer-39 数组中出现次数超过一半的数字 ❀
in Algorithm
- 题目描述:
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9
的数组{1
,2
,3
,2
,2
,2
,5
,4
,2
}。由于数字2
在数组中出现了5
次,超过数组长度的一半,因此输出2
。如果不存在则输出0
。
解题思路:
寻找主元素问题:
1、遍历数组,假设第一个元素为主元素,将其保存,将count
设为1
,之后遇见该元素count++
;否则count--
,count==0
时,切换该位置元素为新的主元素,count
重新置为1
;
2、遍历完成后,有可能是主元素的值已被保存,再遍历一次数组记录下其真实出现次数,大于数组长度的一半即为主元素,否则不存在。
问题图解:

AC代码:
// Find the Number Occurs More Than Half Length in Array
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
if (array==null || array.length<=0) {
return 0;
}
int majorElement = array[0];
int count = 1;
for (int i=1; i<array.length; i++) {
if (array[i] == majorElement) {
count++;
}
else {
count--;
}
if (count == 0) {
majorElement = array[i];
count = 1;
}
}
int numOfMajor = 0;
for (int val : array) {
if (val == majorElement) {
numOfMajor++;
}
}
return numOfMajor>array.length/2 ? majorElement : 0;
}
}
补充说明:
- 这里是牛客编码链接