题目描述:
给定一个大小为 n 的整数数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。
示例 1:
输入:[3,2,3]
输出:[3]
示例 2:
输入:nums = [1]
输出:[1]
示例 3:
输入:[1,1,1,3,3,2,2,2]
输出:[1,2]
代码:
class Solution {
public List majorityElement(int[] nums) {
linkedList ans = new linkedList<>();
Arrays.sort(nums);
int a = 0;
int b = 1;
while(true){
while(bnums.length/3) ans.add(nums[a]);
a = b;
if(a==nums.length) break;
}
return ans;
}
}
运行结果:



