• leetcode 692. Top K Frequent Words(频率最高的K个单词)


    Given an array of strings words and an integer k, return the k most frequent strings.

    Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.

    Example 1:

    Input: words = [“i”,“love”,“leetcode”,“i”,“love”,“coding”], k = 2
    Output: [“i”,“love”]
    Explanation: “i” and “love” are the two most frequent words.
    Note that “i” comes before “love” due to a lower alphabetical order.
    Example 2:

    Input: words = [“the”,“day”,“is”,“sunny”,“the”,“the”,“the”,“sunny”,“is”,“is”], k = 4
    Output: [“the”,“is”,“sunny”,“day”]
    Explanation: “the”, “is”, “sunny” and “day” are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.

    统计words数组中各单词出现的频率,返回频率由高到低的前K个单词。
    频率一样时,按字典顺序排序。

    思路:

    首先要统计每个单词出现的频率,所以要用到hash map.
    遍历一遍words,保存每个单词和对应的频率到hash map.

    然后要按频率由高到低排序,找到对应的单词。
    这步可以用优先队列,定义排序规则:按频率由高到低,频率相同时,按字典顺序排序。

    最后只需要从优先队列中取出前k个单词装入list。

    可以自定义一个class,包含单词和对应的频率
    字典顺序的比较可以用String的compareTo方法。

    public List<String> topKFrequent(String[] words, int k) {
        Queue<Node> heap = new PriorityQueue<>(k, (a, b)->(a.freq == b.freq ? 
                                                          a.word.compareTo(b.word) : b.freq - a.freq));
        HashMap<String, Integer> map = new HashMap<>();
        List<String> res = new ArrayList<>();
        
        for(String word : words) {
            map.put(word, map.getOrDefault(word, 0)+1);
        }
        
        map.forEach((key, val) -> {
            heap.offer(new Node(key, val));
        });
        
        while(k > 0) {
            res.add(heap.poll().word);
            k --;
        }
        return res;
    }
    
    class Node{
        String word;
        int freq;
        
        public Node(String word, int freq) {
            this.word = word;
            this.freq = freq;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    不过自定义的class没有map中的entrySet高效

    public List<String> topKFrequent(String[] words, int k) {
        Queue<Map.Entry<String,Integer>> heap = new PriorityQueue<>((a, b)->(a.getValue() == b.getValue() ? 
                                                          a.getKey().compareTo(b.getKey()) : b.getValue() - a.getValue()));
        HashMap<String, Integer> map = new HashMap<>();
        List<String> res = new ArrayList<>();
        final int num = k;
        
        for(String word : words) {
            map.put(word, map.getOrDefault(word, 0)+1);
        }
        
        for(Map.Entry<String, Integer> entry : map.entrySet()) {
            heap.offer(entry);
        }
        
        while(k > 0) {
            res.add(heap.poll().getKey());
            k --;
        }
        return res;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    [附源码]计算机毕业设计JAVA医院挂号管理系统
    数据结构之栈和队列以及如何封装栈和队列,栈和队列的实例(进制转换和击鼓传花)
    Java框架 MyBatis获取参数值的两种方式
    外包干了3天,技术退步明显.......
    LeetCode117. Populating Next Right Pointers in Each Node II
    Notepad++ 常用
    基于SpringBoot的医疗预约服务管理系统
    Java宝典-抽象类和接口
    Excel画图
    [SpringBoot系列]SpringBoot如何整合SSMP
  • 原文地址:https://blog.csdn.net/level_code/article/details/127401515