forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
51 lines (46 loc) · 1.13 KB
/
Solution.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
48
49
50
51
/**
* @author Anonymous
* @since 2019/12/6
*/
public class Solution {
/**
* 查找数组中出现次数超过一次的数字
*
* @param array 数组
* @return 返回该数,不存在则返回0
*/
public int MoreThanHalfNum_Solution(int[] array) {
if (array == null || array.length == 0) {
return 0;
}
int res = array[0];
int times = 1;
for (int i = 1; i < array.length; ++i) {
if (times == 0) {
res = array[i];
times = 1;
} else if (array[i] == res) {
++times;
} else {
--times;
}
}
return isMoreThanHalf(array, res) ? res : 0;
}
/**
* 判断val元素是否真的超过数组元素个数的一半
*
* @param array 数组
* @param val 某元素
* @return boolean
*/
private boolean isMoreThanHalf(int[] array, int val) {
int cnt = 0;
for (int e : array) {
if (e == val) {
++cnt;
}
}
return cnt * 2 > array.length;
}
}