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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

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

【問(wèn)題描述】355 設(shè)計(jì)推特

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

【解答思路】

1. 設(shè)計(jì)思路


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 和推文(單鏈表)的對(duì)應(yīng)關(guān)系*/private Map<Integer, Tweet> twitter;/*** 用戶 id 和他關(guān)注的用戶列表的對(duì)應(yīng)關(guān)系*/private Map<Integer, Set<Integer>> followings;/*** 全局使用的時(shí)間戳字段,用戶每發(fā)布一條推文之前 + 1*/private static int timestamp = 0;/*** 合并 k 組推文使用的數(shù)據(jù)結(jié)構(gòu)(可以在方法里創(chuàng)建使用),聲明成全局變量非必需,視個(gè)人情況使用*/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();// 如果自己發(fā)了推文也要算上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);// 這里最好的操作應(yīng)該是 replace,但是 Java 沒(méi)有提供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 發(fā)起關(guān)注者 id* @param followeeId 被關(guān)注者 id*/public void follow(int followerId, int followeeId) {// 被關(guān)注人不能是自己if (followeeId == followerId) {return;}// 獲取我自己的關(guān)注列表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 發(fā)起取消關(guān)注的人的 id* @param followeeId 被取消關(guān)注的人的 id*/public void unfollow(int followerId, int followeeId) {if (followeeId == followerId) {return;}// 獲取我自己的關(guān)注列表Set<Integer> followingList = followings.get(followerId);if (followingList == null) {return;}// 這里刪除之前無(wú)需做判斷,因?yàn)椴檎沂欠翊嬖谝院?#xff0c;就可以刪除,反正刪除之前都要查找followingList.remove(followeeId);}/*** 推文類,是一個(gè)單鏈表(結(jié)點(diǎn)視角)*/private class Tweet {/*** 推文 id*/private int id;/*** 發(fā)推文的時(shí)間戳*/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/

【總結(jié)】

1.維護(hù)數(shù)據(jù)
  • 如果需要維護(hù)數(shù)據(jù)的時(shí)間有序性,鏈表在特殊場(chǎng)景下可以勝任。因?yàn)闀r(shí)間屬性通常來(lái)說(shuō)是相對(duì)固定的,而不必去維護(hù)順序性;
  • 如果需要?jiǎng)討B(tài)維護(hù)數(shù)據(jù)有序性,「優(yōu)先隊(duì)列」(堆)是可以勝任的;
2. maxHeap = new PriorityQueue<>((o1, o2) -> -o1.timestamp + o2.timestamp); 中的(o1, o2) -> -o1.timestamp + o2.timestamp
這里是 Java 的 lambda 表達(dá)式,等價(jià)于:Java 代碼:maxHeap = new PriorityQueue<>(new Comparator<Tweet>() {@Overridepublic int compare(Tweet o1, Tweet o2) {return -o1.timestamp + o2.timestamp;} }); lambda 表達(dá)式,它是把函數(shù)作為一個(gè)方法的參數(shù)的一種等價(jià)寫(xiě)法,可以認(rèn)為是「語(yǔ)法糖」。在 IDEA 工具里面寫(xiě)匿名方法,然后再讓 IDEA 幫助優(yōu)化成 lambda 表達(dá)式。
3. 面向?qū)ο?注意細(xì)節(jié) 分級(jí)思考 想好再碼

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

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。