class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<Integer>();
if (nums.length > 2) {
for (int i = 0; i < nums.length; i++) {
if (res.contains(nums[i]))
continue;
else {
int count = 1;
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]) {
count++;
if (count > nums.length / 3) {
res.add(nums[i]);
break;
}
}
}
}
}
}else{
for(int i:nums)
if(!res.contains(i))
res.add(i);
}
return res;
}
}
229. Majority Element II
最新推荐文章于 2024-11-08 23:27:36 发布