首页 > 其他分享 >[LeetCode] LeetCode692. 前K个高频单词

[LeetCode] LeetCode692. 前K个高频单词

时间:2023-12-17 11:11:41浏览次数:23  
标签:map res s1 getValue 单词 heap s2 LeetCode LeetCode692

题目描述

思路

注意是前K个高频单词,就是TopK问题,只能用小根堆找最大的K个元素啊,用大根堆找的就是最小的K个元素了

思路一:

class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        Map<String, Integer> map = new HashMap<>();
        // 小顶堆
        PriorityQueue<Map.Entry<String, Integer> > heap = new PriorityQueue<>(
            (s1, s2) -> {
                if (s1.getValue().equals(s2.getValue())) {
                    return s2.getKey().compareTo(s1.getKey());
                } else {
                    return s1.getValue() - s2.getValue();
                }
            }
        );
        List<String> res = new ArrayList<>();
        for (String str : words) {
            map.put(str, map.getOrDefault(str, 0) + 1);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            heap.add(entry);
            if (heap.size() > k) {
                heap.remove();
            }
        }
        while (!heap.isEmpty()) {
            res.add(heap.remove().getKey());
        }

        Collections.reverse(res);
        return res;
    }
}

标签:map,res,s1,getValue,单词,heap,s2,LeetCode,LeetCode692
From: https://www.cnblogs.com/keyongkang/p/17908869.html

相关文章

  • [LeetCode] LeetCode451. 根据字符出现频率排序
    题目描述思路:使用大顶堆方法一:classSolution{publicStringfrequencySort(Strings){//1.HashMap统计词频Map<Character,Integer>map=newHashMap<>();for(charc:s.toCharArray()){map.put(c,map.getOrDefault(......
  • c++单词排序。
    --START--读入n个英文单词,将这n个单词按字典序升序排序后,重新输出。第1行,一个正整数n。(0<n<100)第2行,n个英文单词。单词之间用空格隔开。一行,n个按字典序升序排序后的英文单词。单词之间用空格隔开。in:5hiIamastudentout:Iamahistudent#include<iostream>......
  • [LeetCode] 2482. Difference Between Ones and Zeros in Row and Column
    Youaregivena0-indexedmxnbinarymatrixgrid.A0-indexedmxndifferencematrixdiffiscreatedwiththefollowingprocedure:LetthenumberofonesintheithrowbeonesRowi.LetthenumberofonesinthejthcolumnbeonesColj.Letthenumbero......
  • [LeetCode] LeeCode703. 数据流中的第K大元素
    题目描述思路:最小堆好好领悟这个代码://将nums数组所有元素插入小根堆中for(intnum:nums){ heap.offer(num); //当小根堆的容量大于k时,就删除堆顶元素 if(heap.size()>k)heap.poll();}当heap.size()==k的时候,还是会再添加一个元素,此时堆内部会已经排好序,......
  • 代码随想录算法训练营第二天| LeetCode977.有序数组的平方、209.长度最小的子数组、59
    LeetCode977.有序数组的平方●今日学习的文章链接和视频链接代码随想录(programmercarl.com) 题目链接977.有序数组的平方-力扣(LeetCode) ●自己看到题目的第一想法昨天正好做了这道题目,总体来说就是用双指针法,要么从绝对值最小的数开始排序,要么从绝对值最大的数开......
  • Google Bard背单词
    promptActasifyouareanexpertonwritingpromptsthathelpuserstolearnvocabulary.Youwillexaminethispromptanduseyourexpertisetotaketheseinstructionsandusethemtoallowuserstolearnvocabulary.Youwillnevertestusers'know......
  • leetcode 209. 长度最小的子数组
    题目:209.长度最小的子数组题目描述:给定一个含有n个正整数的数组和一个正整数target。找出该数组中满足其总和大于等于target的长度最小的连续子数组[numsl,numsl+1,...,numsr-1,numsr],并返回其长度。如果不存在符合条件的子数组,返回0。我的思路:这道题目暴力......
  • 代码随想录算法训练营第一天| LeetCode704 二分查找、27移除元素
     Leetcode704:二分查找今日学习的文章链接:代码随想录(programmercarl.com) 题目链接:704.二分查找-力扣(LeetCode)●  自己看到题目的第一想法这题我会,但是还没明白卡尔说的循环不变量是什么意思。我的固定思路就是,target比中间值大,左指针右移到mid+1;target比中间值......
  • [LeetCode] LeetCode92. 反转链表II
    题目描述思路:同LeetCode25.K个一组翻转链表因为涉及到可能链表的头节点会改变,所以设置dummy节点先走left-1步到达left的左边一个节点查看后面是否存在right-left+1个节点先翻转内部节点指向right-left次再翻转外部节点方法一:/***Definitionforsingly-lin......
  • 【leetcode 239. 滑动窗口最大值】Java优先队列——PriorityQueue类
    leetcode239.滑动窗口最大值题目描述:1e5大小的nums[]数组中长度为k(1<=k<=1e5)的窗口的最大值题解:暴力求解O(n^2)会超时,需要O(nlogn)的解法使用大根堆优先队列维护窗口元素,每次取最大值复杂度降为O(1),堆结构维护复杂度O(logn)问:如果维护窗口[l,r]前[0,l-1]的元素不影......