-
Notifications
You must be signed in to change notification settings - Fork 148
/
MoreThanHalfNum29.java
47 lines (43 loc) · 1.06 KB
/
MoreThanHalfNum29.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.so;
/**
* 第29题
* 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字
*
* @author qgl
* @date 2017/08/14
*/
public class MoreThanHalfNum29 {
/**
* 获取数组中出现次数超过数组长度一半的数字
*
* @param nums
* @return
*/
public static int moreThanHalfNum(int[] nums) {
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}
return checkMoreThanHalf(nums, candidate) ? candidate : 0;
}
/**
* 检查输入的数字出现的次数是否超过数组长度一半
*
* @param array
* @param number
* @return
*/
private static boolean checkMoreThanHalf(int[] array, Integer number) {
int times = 0;
for (int i : array) {
if (i == number) {
times++;
}
}
return times * 2 >= array.length;
}
}