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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[Leedcode][JAVA][第355题][设计推特][面向对象][哈希表][链表][优先队列]

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leedcode][JAVA][第355题][设计推特][面向对象][哈希表][链表][优先队列] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【問題描述】355 設計推特

設計一個簡化版的推特(Twitter),可以讓用戶實現發送推文,關注/取消關注其他用戶,能夠看見關注人(包括自己)的最近十條推文。你的設計需要支持以下的幾個功能:postTweet(userId, tweetId): 創建一條新的推文 getNewsFeed(userId): 檢索最近的十條推文。每個推文都必須是由此用戶關注的人或者是用戶自己發出的。推文必須按照時間順序由最近的開始排序。 follow(followerId, followeeId): 關注一個用戶 unfollow(followerId, followeeId): 取消關注一個用戶 示例:Twitter twitter = new Twitter();// 用戶1發送了一條新推文 (用戶id = 1, 推文id = 5). twitter.postTweet(1, 5);// 用戶1的獲取推文應當返回一個列表,其中包含一個id為5的推文. twitter.getNewsFeed(1); // 用戶1關注了用戶2. twitter.follow(1, 2); // 用戶2發送了一個新推文 (推文id = 6). twitter.postTweet(2, 6); // 用戶1的獲取推文應當返回一個列表,其中包含兩個推文,id分別為 -> [6, 5]. // 推文id6應當在推文id5之前,因為它是在5之后發送的. twitter.getNewsFeed(1); // 用戶1取消關注了用戶2. twitter.unfollow(1, 2); // 用戶1的獲取推文應當返回一個列表,其中包含一個id為5的推文. // 因為用戶1已經不再關注用戶2. twitter.getNewsFeed(1);

【解答思路】

1. 設計思路


import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Set;public class Twitter {/*** 用戶 id 和推文(單鏈表)的對應關系*/private Map<Integer, Tweet> twitter;/*** 用戶 id 和他關注的用戶列表的對應關系*/private Map<Integer, Set<Integer>> followings;/*** 全局使用的時間戳字段,用戶每發布一條推文之前 + 1*/private static int timestamp = 0;/*** 合并 k 組推文使用的數據結構(可以在方法里創建使用),聲明成全局變量非必需,視個人情況使用*/private static PriorityQueue<Tweet> maxHeap;/*** Initialize your data structure here.*/public Twitter() {followings = new HashMap<>();twitter = new HashMap<>();maxHeap = new PriorityQueue<>((o1, o2) -> -o1.timestamp + o2.timestamp);}/*** Compose a new tweet.*/public void postTweet(int userId, int tweetId) {timestamp++;if (twitter.containsKey(userId)) {Tweet oldHead = twitter.get(userId);Tweet newHead = new Tweet(tweetId, timestamp);newHead.next = oldHead;twitter.put(userId, newHead);} else {twitter.put(userId, new Tweet(tweetId, timestamp));}}/*** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.*/public List<Integer> getNewsFeed(int userId) {// 由于是全局使用的,使用之前需要清空maxHeap.clear();// 如果自己發了推文也要算上if (twitter.containsKey(userId)) {maxHeap.offer(twitter.get(userId));}Set<Integer> followingList = followings.get(userId);if (followingList != null && followingList.size() > 0) {for (Integer followingId : followingList) {Tweet tweet = twitter.get(followingId);if (tweet != null) {maxHeap.offer(tweet);}}}List<Integer> res = new ArrayList<>(10);int count = 0;while (!maxHeap.isEmpty() && count < 10) {Tweet head = maxHeap.poll();res.add(head.id);// 這里最好的操作應該是 replace,但是 Java 沒有提供if (head.next != null) {maxHeap.offer(head.next);}count++;}return res;}/*** Follower follows a followee. If the operation is invalid, it should be a no-op.** @param followerId 發起關注者 id* @param followeeId 被關注者 id*/public void follow(int followerId, int followeeId) {// 被關注人不能是自己if (followeeId == followerId) {return;}// 獲取我自己的關注列表Set<Integer> followingList = followings.get(followerId);if (followingList == null) {Set<Integer> init = new HashSet<>();init.add(followeeId);followings.put(followerId, init);} else {if (followingList.contains(followeeId)) {return;}followingList.add(followeeId);}}/*** Follower unfollows a followee. If the operation is invalid, it should be a no-op.** @param followerId 發起取消關注的人的 id* @param followeeId 被取消關注的人的 id*/public void unfollow(int followerId, int followeeId) {if (followeeId == followerId) {return;}// 獲取我自己的關注列表Set<Integer> followingList = followings.get(followerId);if (followingList == null) {return;}// 這里刪除之前無需做判斷,因為查找是否存在以后,就可以刪除,反正刪除之前都要查找followingList.remove(followeeId);}/*** 推文類,是一個單鏈表(結點視角)*/private class Tweet {/*** 推文 id*/private int id;/*** 發推文的時間戳*/private int timestamp;private Tweet next;public Tweet(int id, int timestamp) {this.id = id;this.timestamp = timestamp;}}public static void main(String[] args) {Twitter twitter = new Twitter();twitter.postTweet(1, 1);List<Integer> res1 = twitter.getNewsFeed(1);System.out.println(res1);twitter.follow(2, 1);List<Integer> res2 = twitter.getNewsFeed(2);System.out.println(res2);twitter.unfollow(2, 1);List<Integer> res3 = twitter.getNewsFeed(2);System.out.println(res3);}作者:liweiwei1419 鏈接:https://leetcode-cn.com/problems/design-twitter/solution/ha-xi-biao-lian-biao-you-xian-dui-lie-java-by-liwe/

【總結】

1.維護數據
  • 如果需要維護數據的時間有序性,鏈表在特殊場景下可以勝任。因為時間屬性通常來說是相對固定的,而不必去維護順序性;
  • 如果需要動態維護數據有序性,「優先隊列」(堆)是可以勝任的;
2. maxHeap = new PriorityQueue<>((o1, o2) -> -o1.timestamp + o2.timestamp); 中的(o1, o2) -> -o1.timestamp + o2.timestamp
這里是 Java 的 lambda 表達式,等價于:Java 代碼:maxHeap = new PriorityQueue<>(new Comparator<Tweet>() {@Overridepublic int compare(Tweet o1, Tweet o2) {return -o1.timestamp + o2.timestamp;} }); lambda 表達式,它是把函數作為一個方法的參數的一種等價寫法,可以認為是「語法糖」。在 IDEA 工具里面寫匿名方法,然后再讓 IDEA 幫助優化成 lambda 表達式。
3. 面向對象 注意細節 分級思考 想好再碼

原文鏈接;https://leetcode-cn.com/problems/design-twitter/solution/ha-xi-biao-lian-biao-you-xian-dui-lie-java-by-liwe/

總結

以上是生活随笔為你收集整理的[Leedcode][JAVA][第355题][设计推特][面向对象][哈希表][链表][优先队列]的全部內容,希望文章能夠幫你解決所遇到的問題。

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