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

该博客介绍了一种统计字符串数组中单词频率的方法,通过优先队列和哈希映射来找出出现最频繁的K个单词。首先,利用哈希映射统计每个单词的出现次数,然后用优先队列按频率降序及字典序升序对单词进行排序,最终取出前K个高频单词。此方法适用于数据结构和算法的学习与实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
}

不过自定义的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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值