zoukankan      html  css  js  c++  java
  • lc347 解法

    快速排序版
     
    class Solution {
        public int[] topKFrequent(int[] nums, int k) {
            Map<Integer,Integer> map=new HashMap<Integer,Integer>();
            for(int n:nums){
                if(map.containsKey(n)){
                    map.put(n,map.get(n)+1);
                }else{
                    map.put(n,1);
                }
            }
            List<int[]> values=new ArrayList<>();
            for(Map.Entry<Integer,Integer> entry:map.entrySet()){
                int num = entry.getKey(), count = entry.getValue();
                values.add(new int[]{num, count});
    
            }
            int[] res=new int[k];
            qsort(values,0,values.size()-1,k-1,res);
            return res;
        }
        public void qsort(List<int[]> values,int lo,int hi,int k,int[] res){
            int j=part(values,lo,hi);
            if(j==k){
                for(int i=0;i<=k;i++){
                    res[i]=values.get(i)[0];
                }
                return;
            }else if(j<k){
    
                qsort(values,j+1,hi,k,res);
            }else{
                qsort(values,lo,j-1,k,res);
            }
            
        }
        public int part(List<int[]> values,int lo,int hi){
            int pivot=values.get(lo)[1],l=lo-1,r=hi+1,index=lo;
            while(index<r){
                if(values.get(index)[1]>pivot){
                    Collections.swap(values,index,++l);
                }
                else if(values.get(index)[1]<pivot){
                    Collections.swap(values,index,--r);
                }
                else{
                    index++;
                }
    
            }
            
            return index-1;
    
        }
    
        
    }

    小根堆版

    class Solution {
        public int[] topKFrequent(int[] nums, int k) {
            Map<Integer,Integer> map=new HashMap<Integer,Integer>();
            for(int n:nums){
                if(map.containsKey(n)){
                    map.put(n,map.get(n)+1);
                }else{
                    map.put(n,1);
                }
            }
            PriorityQueue<int[]> pq=new PriorityQueue<int[]>(new Comparator<int[]>(){
                public int compare(int[] m,int[] n){
                    return m[1]-n[1];
                }
            });
            for(Map.Entry<Integer,Integer> entry:map.entrySet()){
                int num = entry.getKey(), count = entry.getValue();
                if(pq.size()==k){
                    if(count>pq.peek()[1]){
                        pq.poll();
                        pq.add(new int[]{num,count});
                    }
    
                }else{
                    pq.add(new int[]{num,count});
                }
    
    
            }
            int[] res=new int[k];
            int i=0;
            while(!pq.isEmpty()){
                int[] r=pq.poll();
                res[i]=r[0];
                i++;
            }
            return res;
        }
        
    
    
        
    }

     桶排序版本

    class Solution {
        public int[] topKFrequent(int[] nums, int k) {
            Map<Integer,Integer> map=new HashMap<Integer,Integer>();
            for(int n:nums){
                if(map.containsKey(n)){
                    map.put(n,map.get(n)+1);
                }else{
                    map.put(n,1);
                }
            }
            List<Integer>[] bucket=new ArrayList[nums.length+1]; 
            for(Map.Entry<Integer,Integer> entry:map.entrySet()){
                int num = entry.getKey(), count = entry.getValue();
                if(bucket[count]==null) bucket[count]=new ArrayList<>();
                bucket[count].add(num);
            }
            int[] res=new int[k];
            int i=nums.length,index=0;
            while(index<k){
                int j=0;
                while(index<k&&bucket[i]!=null&&j<bucket[i].size()){
                    res[index++]=bucket[i].get(j++);
                }
                i--;
            }
            return res;
        }
        
    
    
        
    }
  • 相关阅读:
    Struts2SpringHibernate整合示例,一个HelloWorld版的在线书店(项目源码+详尽注释+单元测试)
    Java实现蓝桥杯勇者斗恶龙
    Java实现 LeetCode 226 翻转二叉树
    Java实现 LeetCode 226 翻转二叉树
    Java实现 LeetCode 226 翻转二叉树
    Java实现 LeetCode 225 用队列实现栈
    Java实现 LeetCode 225 用队列实现栈
    Java实现 LeetCode 225 用队列实现栈
    Java实现 LeetCode 224 基本计算器
    Java实现 LeetCode 224 基本计算器
  • 原文地址:https://www.cnblogs.com/ljf-0/p/13773098.html
Copyright © 2011-2022 走看看