355. 设计推特

痛定思痛。 2023-07-25 06:12 23阅读 0赞

题目描述:

设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能:

  1. postTweet(userId, tweetId): 创建一条新的推文
  2. getNewsFeed(userId): 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。推文必须按照时间顺序由最近的开始排序。
  3. follow(followerId, followeeId): 关注一个用户
  4. unfollow(followerId, followeeId): 取消关注一个用户

示例:

  1. Twitter twitter = new Twitter();
  2. // 用户1发送了一条新推文 (用户id = 1, 推文id = 5).
  3. twitter.postTweet(1, 5);
  4. // 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
  5. twitter.getNewsFeed(1);
  6. // 用户1关注了用户2.
  7. twitter.follow(1, 2);
  8. // 用户2发送了一个新推文 (推文id = 6).
  9. twitter.postTweet(2, 6);
  10. // 用户1的获取推文应当返回一个列表,其中包含两个推文,id分别为 -> [6, 5].
  11. // 推文id6应当在推文id5之前,因为它是在5之后发送的.
  12. twitter.getNewsFeed(1);
  13. // 用户1取消关注了用户2.
  14. twitter.unfollow(1, 2);
  15. // 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
  16. // 因为用户1已经不再关注用户2.
  17. twitter.getNewsFeed(1);

解题思路:

来源于: He-haitao

k个排序链表的合并,改成1(自己最近10条推特)+k个关注用户的最近10条推特的合并,因为是常数个,所以问题不大,这边给一个双字典加排序写的。


代码:

  1. class Twitter(object):
  2. def __init__(self):
  3. """
  4. Initialize your data structure here.
  5. """
  6. self.count=0
  7. self.user_follow = collections.defaultdict(set)
  8. self.user_post = collections.defaultdict(list)
  9. def postTweet(self, userId, tweetId):
  10. """
  11. Compose a new tweet.
  12. """
  13. self.user_post[userId].append([self.count,tweetId])
  14. self.count+=1
  15. def getNewsFeed(self, userId):
  16. """
  17. 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.
  18. """
  19. persons = self.user_follow[userId]
  20. temp = []
  21. temp.extend(self.user_post[userId][-10:])
  22. for person in persons:
  23. temp.extend(self.user_post[person][-10:])
  24. temp.sort(key=lambda x:-x[0])
  25. res = []
  26. for ele in temp[:10]:
  27. res.append(ele[1])
  28. return res
  29. def follow(self, followerId, followeeId):
  30. """
  31. Follower follows a followee. If the operation is invalid, it should be a no-op.
  32. """
  33. if followeeId!=followerId:
  34. self.user_follow[followerId].add(followeeId)
  35. def unfollow(self, followerId, followeeId):
  36. """
  37. Follower unfollows a followee. If the operation is invalid, it should be a no-op.
  38. """
  39. if followeeId in self.user_follow[followerId]:
  40. self.user_follow[followerId].remove(followeeId)

参考链接:

https://leetcode-cn.com/problems/design-twitter/solution/355-she-ji-tui-te-88ms98-by-tuotuoli/


题目来源:

https://leetcode-cn.com/problems/design-twitter

发表评论

表情:
评论列表 (有 0 条评论,23人围观)

还没有评论,来说两句吧...

相关阅读

    相关 每日一题-设计

    今天是2020年4月13日,星期一。 题目描述 设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近

    相关 355. 设计

    题目描述: 设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能:

    相关 leetcode355 设计

    设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能: postT

    相关 Twitter高级搜索

    今天,尝试通过模拟浏览器爬取推特数据。想要爬取包括不同关键词的推文,比如含有“home”或者“school”其中的一个,再或者需要指定发推的时间,那么我们需要用到推特的高级搜索