LeetCode#229|| -求众数
package shuzu; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class p229 { public static List<Integer> majorityElement(int[] nums) { List<Integer> list=new ArrayList<>(); if(nums==null||nums.length==0)return list; Arrays.sort(nums); int count=1; for(int i=1;i<nums.length;i++){ if(nums[i]==nums[i-1]){ count++; } else { //System.out.print("-----------"+count); if(count>nums.length/3)list.add(nums[i-1]); count=1; } } if(count>nums.length/3)list.add(nums[nums.length-1]); return list; } public static void main(String[] args) { int mnum[]={3,2,3}; System.out.println(majorityElement(mnum)); } }
云香精结果: