LeetCode:前K个高频单词【692】

题目描述

给一非空的单词列表,返回前 个出现次数最多的单词。

返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。

示例 1:

输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
输出: ["i", "love"]
解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。
注意,按字母顺序 "i" 在 "love" 之前。

示例 2:

输入: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
输出: ["the", "is", "sunny", "day"]
解析: "the", "is", "sunny" 和 "day" 是出现次数最多的四个单词,
出现次数依次为 4, 3, 2 和 1 次。

注意:

  1. 假定 k 总为有效值, 1 ≤ k ≤ 集合元素数。
  2. 输入的单词均由小写字母组成。

题目分析

  这道题是前K个高频元素【347】的进阶。难度主要增加在对结果的处理:

  • 首先,结果按照单词的出现次数递减排列。
  • 相同频率的单词按照子母序列排序。

  我们先处理第一个问题,出现次数递减,我们知道,优先队列的是小顶堆,转换为列表后呈递增形式,我们使用集合方法将它逆转即可

  然后是第二个问题,我们在最后会对取出来的集合进行逆转操作,所以我们在设计比较器的时候,要让字母序大的在前面,比如a,b的出现次数相同,我们在比较器中先让b处在a的前面,最后逆转操作时,a就到了b的前面

Java题解

class Solution {
public List<String> topKFrequent(String[] words, int k) {
Map<String,Integer> countMap = new HashMap<>();
for(String word:words)
countMap.put(word,countMap.getOrDefault(word,0)+1); PriorityQueue<String> priorityQueue = new PriorityQueue<>(k, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return countMap.get(o1).equals(countMap.get(o2))?o2.compareTo(o1):countMap.get(o1)-countMap.get(o2);
}
});
for (String word : countMap.keySet()) {
priorityQueue.offer(word);
if (priorityQueue.size() > k) {
priorityQueue.poll();
}
}
List<String> ans = new ArrayList<>();
while (!priorityQueue.isEmpty()) ans.add(priorityQueue.poll());
Collections.reverse(ans);
return ans;
}
}

LeetCode:前K个高频单词【692】的更多相关文章

  1. Java实现 LeetCode 692 前K个高频单词(map的应用)

    692. 前K个高频单词 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. 示例 1: 输入: [&qu ...

  2. 692. 前K个高频单词

    2021-05-20 LeetCode每日一题 链接:https://leetcode-cn.com/problems/top-k-frequent-words/ 标签:堆.字典序.哈希表 题目 给一 ...

  3. [Swift]LeetCode692. 前K个高频单词 | Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  4. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  5. [LeetCode] Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  6. 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)

    目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...

  7. LeetCode:前K个高频元素【347】

    LeetCode:前K个高频元素[347] 题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [ ...

  8. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  9. Java实现 LeetCode 347 前 K 个高频元素

    347. 前 K 个高频元素 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输 ...

随机推荐

  1. pl/sql 实例精解 07

    这章主要讨论 oracle11g 新特性, continue, continue when 语句 continue 的作用同其他编程语言一样. continue when condition 只是当条 ...

  2. PAT006 Tree Traversals Again

    题目: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exa ...

  3. 根本上解决npm install 报错“ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.“

    每次项目npm install 的时候都报这个错误, 然后网上找的方法就把这个 ajv重新安装下,感觉有点麻烦, 后来有次我把npm更新了一下(我的版本是: 6.1.0),更新到了最新版本,这个问题就 ...

  4. 【python】字符串编码问题

    参考:http://blog.csdn.net/tingsking18/article/details/4033645 python内部的字符串是以unicode来编码 decode函数用来将其他编码 ...

  5. Laravel5.1 搭建博客 --展示简单的首页

    今天起开始搭建博客,把之前学的东西运用下. 1 创建 配置项目 1.1 创建项目 composer create-project laravel/laravel blog 5.1.1 1.2 配置数据 ...

  6. Using Notepad++ To Quickly Format XML

    http://geek.sylvainw.com/2010/03/28/using-notepad-to-quickly-format-xml/ My favorite way to quickly ...

  7. Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授

     本文转载至 http://blog.csdn.net/woaifen3344/article/details/41311023 Code3000极光推送erroryour certificate n ...

  8. fis3 静态文件 发布 线上

    PMS上线及安装文档 目录 一:安装环境1 1.1安装Node和NPM 1 1.2安装FIS3 1 1.3安装fis3压缩包 1 二:上线更新2 1:发布网站 2 2:寻找fis3 文件 2 3:复制 ...

  9. C#中遍历ArrayList的三种方法

    using System; using System.Collections; using System.Linq; using System.Text; namespace ArrayListDem ...

  10. mysql查询某天是本年第几周

    SELECT WEEK(date_add('2012-12-31',interval 6 day),2);