【一次过】【Comparator】Lintcode 471. 最高频的K个单词

给一个单词列表,求出这个列表中出现频次最高的K个单词。

样例

给出单词列表:

[
    "yes", "lint", "code",
    "yes", "code", "baby",
    "you", "baby", "chrome",
    "safari", "lint", "code",
    "body", "lint", "code"
]

如果 k = 3, 返回 ["code", "lint", "baby"]
如果 k = 4, 返回 ["code", "lint", "baby", "yes"]

挑战

用 O(n log k)的时间和 O(n) 的额外空间完成。


解题思路:

先用哈希表统计出每个单词的频次,然后将单词与频次信息组合成对象放入TreeSet中,其中TreeSet需要自定义排序,先按照频次降序,然后按照字典序升序。最后将TreeSet中前K个元素取出来即可。

public class Solution {
    /**
     * @param words: an array of string
     * @param k: An integer
     * @return: an array of string
     */
    public String[] topKFrequentWords(String[] words, int k) {
        // write your code here
        Map<String , Integer> map = new HashMap<>();
        for(String s : words){
            if(map.get(s) == null)
                map.put(s, 1);
            else
                map.put(s, map.get(s) + 1);
        }
        
        TreeSet<Pair> set = new TreeSet<>(new Comparator<Pair>(){
            @Override
            public int compare(Pair a, Pair b){
                //按频次降序
                if(a.times != b.times)
                    return b.times - a.times;
                //按字典升序
                return a.str.compareTo(b.str);
            }
        });
        
        for(String s : map.keySet())
            set.add(new Pair(s, map.get(s)));
        
        String[] res = new String[k];
        for(int j=0; j<k && !set.isEmpty(); j++)
            res[j] = set.pollFirst().str;
        
        return res;
    }
    
    class Pair{
        public String str;
        public int times;
        
        public Pair(String str, int times){
            this.str = str;
            this.times = times;
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值