-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.java
41 lines (34 loc) · 1017 Bytes
/
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
package leetcode.algo.misc.leet_zh_169;
public class Solution {
/*
*
* 169. 求众数
*
* 摩尔根投票法
*
* 执行用时 : 3 ms, 在Majority Element的Java提交中击败了97.16% 的用户
* 内存消耗 : 50.2 MB, 在Majority Element的Java提交中击败了28.03% 的用户
* */
// 摩尔根投票法
public int majorityElement(int[] nums) {
// 题干:你可以假设数组是非空的,并且给定的数组总是存在众数。
int a = -1, size = 0;
for (int num : nums) {
if (size == 0) {
a = num;
size++;
} else if (a == num)
size++;
else {
size--;
}
}
return a;
}
public static void main(String[] args) {
Solution ss = new Solution();
// int[] nums = {3, 2, 3};
int[] nums = {2, 2, 1, 1, 1, 2, 2};
System.out.println(ss.majorityElement(nums));
}
}