Problem
Given a non-empty array of integers, return the k most frequent elements.
ExampleGiven [1,1,1,2,2,3] and k = 2, return [1,2].
NoteYou may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm"s time complexity must be better than O(n log n), where n is the array"s size.
public class Solution { public ListUpdate 2018-9 PriorityQueuetopKFrequent(int[] nums, int k) { int n = nums.length; Map map = new HashMap<>(); for (int num: nums) { if (map.containsKey(num)) map.put(num, map.get(num)+1); else map.put(num, 1); } List [] freq = new ArrayList[n+1]; for (int num : map.keySet()) { int i = map.get(num); if (freq[i] == null) { freq[i] = new ArrayList<>(); } freq[i].add(num); } List res = new ArrayList<>(); for (int index = freq.length - 1; index >= 0 && res.size() < k; index--) { if (freq[index] != null) { res.addAll(freq[index]); } } return res; } }
class Solution { public ListBucket sorttopKFrequent(int[] nums, int k) { List res = new ArrayList<>(); if (nums == null || nums.length < k) return res; Map map = new HashMap<>(); PriorityQueue > queue = new PriorityQueue<>((a, b)->b.getValue()-a.getValue()); for (int num: nums) { map.put(num, map.getOrDefault(num, 0)+1); } for (Map.Entry entry: map.entrySet()) { queue.offer(entry); } int count = 0; while (count < k) { Map.Entry entry = queue.poll(); res.add(entry.getKey()); count++; } return res; } }
class Solution { public ListtopKFrequent(int[] nums, int k) { List res = new ArrayList<>(); if (nums == null || nums.length < k) return res; Map map = new HashMap<>(); for (int num: nums) { map.put(num, map.getOrDefault(num, 0)+1); } List[] buckets = new ArrayList[nums.length+1]; for (Map.Entry entry: map.entrySet()) { int value = entry.getValue(); if (buckets[value] == null) buckets[value] = new ArrayList<>(); buckets[value].add(entry.getKey()); } for (int i = buckets.length-1; i >= 0 && res.size() < k; i--) { if (buckets[i] != null) res.addAll(buckets[i]); } return res; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65995.html
摘要:描述给定一个非空的整数数组,返回其中出现频率前高的元素。然后以元素出现的次数为值,统计该次数下出现的所有的元素。从最大次数遍历到次,若该次数下有元素出现,提取该次数下的所有元素到结果数组中,知道提取到个元素为止。 Description Given a non-empty array of integers, return the k most frequent elements. E...
摘要:题目要求假设有一个非空的整数数组,从中获得前个出现频率最多的数字。先用来统计出现次数,然后将其丢到对应的桶中,最后从最高的桶开始向低的桶逐个遍历,取出前个频率的数字。 题目要求 Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,...
摘要:先按照元素次数的将所有元素存入,再按照次数元素将哈希表里的所有元素存入,然后取最后的个元素返回。 Problem Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: ...
LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...
摘要:例如题目解析题目的意思很明显,就是把两个数字加起来,需要考虑进位的情况。总结这个题目如果都能独立完成,那么水平已经可以足以应付国内各大企业的算法面。 欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由落影发表 前言 LeetCode上的题目是大公司面试常见的算法题,今天的目标是拿下5道算法题: 题目1是基于链表的大数加法,既考察基本数据结构的了解,又考察在处理加法过程中...
阅读 882·2021-11-15 11:38
阅读 1602·2021-09-24 09:48
阅读 840·2021-09-24 09:47
阅读 2271·2021-08-26 14:15
阅读 3499·2019-08-30 11:09
阅读 2605·2019-08-29 16:55
阅读 1585·2019-08-26 14:01
阅读 3035·2019-08-23 16:47