355. Design Twitter
/** 355. Design Twitter https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the 10 most recent tweets in the user's news feed. Implement the Twitter class: 1. Twitter() Initializes your twitter object. 2. void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId by the user userId. Each call to this function will be made with a unique tweetId. 3. List<Integer> getNewsFeed(int userId) Retrieves 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 themself. Tweets must be ordered from most recent to least recent. 4. void follow(int followerId, int followeeId) The user with ID followerId started following the user with ID followeeId. 5. void unfollow(int followerId, int followeeId) The user with ID followerId started unfollowing the user with ID followeeId. Example 1: Input ["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"] [[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]] Output [null, null, [5], null, null, [6, 5], null, [5]] Explanation Twitter twitter = new Twitter(); twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5). twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5]. return [5] twitter.follow(1, 2); // User 1 follows user 2. twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6). twitter.getNewsFeed(1); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5. twitter.unfollow(1, 2); // User 1 unfollows user 2. twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5], since user 1 is no longer following user 2. Constraints: 1. 1 <= userId, followerId, followeeId <= 500 2. 0 <= tweetId <= 10^4 3. All the tweets have unique IDs. 4. At most 3 * 10^4 calls will be made to postTweet, getNewsFeed, follow, and unfollow. */ use std::collections::{ HashMap, HashSet, LinkedList, }; type user_id = i32; type tweet_id = i32; pub struct Twitter { tweets: LinkedList<(user_id, tweet_id)>, relations: HashMap<user_id, HashSet<user_id>>, } /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl Twitter { /* Solution: LinkedList to save tweets, HashMap to save relations */ pub fn new() -> Self { Twitter { tweets: LinkedList::new(), relations: HashMap::new(), } } /** Composes a new tweet with ID tweetId by the user userId. Each call to this function will be made with a unique tweetId. */ pub fn post_tweet(&mut self, user_id: i32, tweet_id: i32) { self.tweets.push_front((user_id, tweet_id)); } /** Retrieves 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 themself. Tweets must be ordered from most recent to least recent. */ pub fn get_news_feed(&mut self, user_id: i32) -> Vec<i32> { //take out tweets by user_id let mut result: Vec<i32> = self.tweets.iter().filter(|item| { if item.0 == user_id { return true; } //take out tweets of user of user_id's following match self.relations.get(&user_id) { Some(set) => return set.contains(&item.0), None => return false, } }).take(10).map(|item| item.1).collect(); result } /** The user with ID followerId started following the user with ID followeeId. */ pub fn follow(&mut self, follower_id: i32, followee_id: i32) { self.relations.entry(follower_id).or_insert(HashSet::new()).insert(followee_id); } /** The user with ID followerId started unfollowing the user with ID followeeId. */ fn unfollow(&mut self, follower_id: i32, followee_id: i32) { /* or_insert: Ensures a value is in the entry by inserting the default if empty, and returns a mutable reference to the value in the entry. */ self.relations.entry(follower_id).or_insert(HashSet::new()).remove(&followee_id); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-12-13 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
2020-12-13 1688. Count of Matches in Tournament
2019-12-13 272. Closest Binary Search Tree Value II