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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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...
摘要:例如題目解析題目的意思很明顯,就是把兩個數字加起來,需要考慮進位的情況??偨Y這個題目如果都能獨立完成,那么水平已經可以足以應付國內各大企業的算法面。 歡迎大家前往騰訊云+社區,獲取更多騰訊海量技術實踐干貨哦~ 本文由落影發表 前言 LeetCode上的題目是大公司面試常見的算法題,今天的目標是拿下5道算法題: 題目1是基于鏈表的大數加法,既考察基本數據結構的了解,又考察在處理加法過程中...
閱讀 880·2021-11-15 11:38
閱讀 1600·2021-09-24 09:48
閱讀 838·2021-09-24 09:47
閱讀 2270·2021-08-26 14:15
閱讀 3497·2019-08-30 11:09
閱讀 2602·2019-08-29 16:55
閱讀 1582·2019-08-26 14:01
閱讀 3033·2019-08-23 16:47