Leetcode 355. 设计推特 C++

    技术2022-07-11  84

    Leetcode 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);

    题解

    一个数组list按照时间顺序存储所有推特,用一个哈希表tweetUser记录发送推特的用户名,用一个哈希表user记录用户关注的用户名。 发送推特,只需要更新list、tweetUser即可 关注、取消关注只需要更新user即可 查找推特时,我们对list从右往前进行查找,只要是用户自己或关注的用户发的,就存入数组中 详细过程见代码

    代码

    class Twitter { public: unordered_map<int,unordered_set<int>> user; unordered_map<int,int> tweetUser; vector<int> list; /** Initialize your data structure here. */ Twitter() { } /** Compose a new tweet. */ void postTweet(int userId, int tweetId) { list.push_back(tweetId); tweetUser[tweetId] = userId; } /** 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. */ vector<int> getNewsFeed(int userId) { vector<int> result; int num=0; for(int i=list.size()-1; i>=0&&num<10; i--){ if(tweetUser[list[i]]==userId || user[userId].find(tweetUser[list[i]]) != user[userId].end()){ //第一个条件是判断是不是自己发送的推特,第二个条件是判断是不是关注的人发送的推特 result.push_back(list[i]); num++; } } return result; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ void follow(int followerId, int followeeId) { user[followerId].insert(followeeId); } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ void unfollow(int followerId, int followeeId) { user[followerId].erase(followeeId); } }; /** * Your Twitter object will be instantiated and called as such: * Twitter* obj = new Twitter(); * obj->postTweet(userId,tweetId); * vector<int> param_2 = obj->getNewsFeed(userId); * obj->follow(followerId,followeeId); * obj->unfollow(followerId,followeeId); */

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/design-twitter 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    Processed: 0.015, SQL: 9