题目
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
答案
class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> list = new ArrayList<>();
int candidate1 = 0, candidate2 = 1, count1 = 0, count2 = 0;
for(int x : nums) {
if(x == candidate1)
count1++;
else if(x == candidate2)
count2++;
else if(count1 == 0) {
candidate1 = x;
count1 = 1;
}
else if(count2 == 0) {
candidate2 = x;
count2 = 1;
}
else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
for(int x : nums) {
if(x == candidate1) count1++;
if(x == candidate2) count2++;
}
if(count1 > nums.length / 3) list.add(candidate1);
if(count2 > nums.length / 3) list.add(candidate2);
return list;
}
}
网友评论