日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Leetcode][第347题][JAVA][前K个高频元素][优先队列][堆][遍历set/map]

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leetcode][第347题][JAVA][前K个高频元素][优先队列][堆][遍历set/map] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【問題描述】[中等]

【解答思路】

1. 堆

復雜度

class Solution {public int[] topKFrequent(int[] nums, int k) {Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>();for (int num : nums) {occurrences.put(num, occurrences.getOrDefault(num, 0) + 1);}// int[] 的第一個元素代表數組的值,第二個元素代表了該值出現的次數PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {public int compare(int[] m, int[] n) {return m[1] - n[1];}});for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) {int num = entry.getKey(), count = entry.getValue();if (queue.size() == k) {if (queue.peek()[1] < count) {queue.poll();queue.offer(new int[]{num, count});}} else {queue.offer(new int[]{num, count});}}int[] ret = new int[k];for (int i = 0; i < k; ++i) {ret[i] = queue.poll()[0];}return ret;} } class Solution {public int[] topKFrequent(int[] nums, int k) {Map<Integer,Integer> map =new HashMap<>();for(int num:nums){map.put(num,map.getOrDefault(num,0)+1);}PriorityQueue<Integer> queue = new PriorityQueue<>((a,b)->map.get(a)-map.get(b));/*PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {@Overridepublic int compare(Integer a, Integer b) {return map.get(b) -map.get(a);}});*/for(int key:map.keySet()){queue.add(key);if(queue.size()>k){queue.poll();}}int res[]=new int[k];int index=0;while(k>0){res[index++]=queue.poll();k--;}return res;} }

【總結】

1. 大小堆的建立(其他類比)

1.1 Map的小堆
PriorityQueue queue = new PriorityQueue<>((a,b)->map.get(a)-map.get(b));

1.2 Map的大堆

//idea Comparator 自動生成 PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {@Overridepublic int compare(Integer a, Integer b) {return map.get(b) -map.get(a);}});

1.3 分開寫

// Customer 一個class 含id 7 初始化大小 Queue<Customer> customerPriorityQueue = new PriorityQueue<>(7, idComparator); //匿名Comparator實現public static Comparator<Customer> idComparator = new Comparator<Customer>(){@Overridepublic int compare(Customer c1, Customer c2) {return (int) (c1.getId() - c2.getId());}};
2.遍歷map /set

map

public static void main(String[] args) {// 構建一個Map 初始值為3條數據Map<String, String> map = new HashMap<String, String>();map.put("1", "xiaqiu");map.put("2", "pangzi");map.put("3", "shouzi");//第一種:普遍使用,二次取值System.out.println("通過Map.keySet遍歷key和value:");for (String key : map.keySet()) {System.out.println("key= "+ key + " and value= " + map.get(key));}//第二種:通過Iterator迭代器遍歷循環Map.entrySet().iterator();System.out.println("通過Map.entrySet使用iterator遍歷key和value:");Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();while (it.hasNext()) {Map.Entry<String, String> entry = it.next();System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}//第三種:筆者推薦,尤其是容量大時(相對來說 比2好一點 效率高)System.out.println("通過Map.entrySet遍歷key和value");for (Map.Entry<String, String> entry : map.entrySet()) {System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());}//第四種System.out.println("通過Map.values()遍歷所有的value,但不能遍歷key");for (String v : map.values()) {System.out.println("value= " + v);}}

set

1.迭代遍歷: Set<String> set = new HashSet<String>(); Iterator<String> it = set.iterator(); while (it.hasNext()) { String str = it.next(); System.out.println(str); } 2.for循環遍歷: for (String str : set) { System.out.println(str); }
3.堆的思想

【數據結構與算法】堆

參考鏈接:https://www.cnblogs.com/magicya/p/6683052.html
參考鏈接:https://www.cnblogs.com/XQiu/p/5087961.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的[Leetcode][第347题][JAVA][前K个高频元素][优先队列][堆][遍历set/map]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。