leetcode355 设计推特

快来打我* 2023-07-06 09:52 75阅读 0赞

设计一个简化版的推特(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);

这题刚开始想直接用LinkedList,直接去后面的插入的元素,但是关注的人数很多,不能区分,于是我从评论哪里学到,利用链表,头接法,十分好使,同时出色的设计,能大幅减少系统的性能资源的消耗,学习了。

  1. class Twitter {
  2. class Tweet{
  3. int id;
  4. int time;
  5. Tweet next;
  6. public Tweet(int id,int time) {
  7. this.id = id;
  8. this.time = time;
  9. this.next = null;
  10. }
  11. }
  12. // 关注列表
  13. Map<Integer,HashSet<Integer>> foll;
  14. // 推文列表
  15. Map<Integer,Tweet> news;
  16. static int timestamp;
  17. /** Initialize your data structure here. */
  18. public Twitter() {
  19. foll = new HashMap<>();
  20. news = new HashMap<>();
  21. timestamp = 0;
  22. }
  23. /** Compose a new tweet. */
  24. public void postTweet(int userId, int tweetId) {
  25. // 维护推文列表
  26. Tweet tweet = new Tweet(tweetId,timestamp++);
  27. Tweet head = news.get(userId);
  28. tweet.next = head;
  29. head = tweet;
  30. news.put(userId,head);
  31. // 自己关注自己
  32. if (!foll.containsKey(userId)) {
  33. HashSet<Integer> set = new HashSet<>();
  34. set.add(userId);
  35. foll.put(userId,set);
  36. }
  37. }
  38. /** 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. */
  39. //检索最近的十条推文,推文必须按照时间顺序由最近的开始排序。
  40. public List<Integer> getNewsFeed(int userId) {
  41. List<Integer> res = new ArrayList<>();
  42. List<Tweet> tweets = new ArrayList<>();
  43. HashSet<Integer> users = foll.get(userId);
  44. if (users == null || users.size() == 0) {
  45. return res;
  46. }
  47. // 放入tweets中
  48. for (Integer user:users) {
  49. tweets.add(news.get(user));
  50. }
  51. // 寻找10个
  52. for (int i = 0;i < 10;i++) {
  53. int max_time = Integer.MIN_VALUE;
  54. int max_id = -1;
  55. for (int j = 0;j < tweets.size();j++) {
  56. Tweet tw = tweets.get(j);
  57. if (tw == null) continue;
  58. if (tw.time > max_time) {
  59. max_time = tw.time;
  60. max_id = j;
  61. }
  62. }
  63. if (max_id >= 0) {
  64. res.add(tweets.get(max_id).id);
  65. tweets.set(max_id,tweets.get(max_id).next);
  66. }
  67. }
  68. return res;
  69. }
  70. /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
  71. // 关注一个用户
  72. public void follow(int followerId, int followeeId) {
  73. // 关注列表
  74. if (followerId == followeeId ) return;
  75. if (foll.containsKey(followerId)) {
  76. HashSet<Integer> set = foll.get(followerId);
  77. set.add(followeeId);
  78. } else {
  79. HashSet<Integer> set = new HashSet<>();
  80. set.add(followerId);
  81. set.add(followeeId);
  82. foll.put(followerId,set);
  83. }
  84. }
  85. /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
  86. // 取消关注一个用户
  87. public void unfollow(int followerId, int followeeId) {
  88. if (followerId == followeeId) return;
  89. if (foll.containsKey(followerId)) {
  90. HashSet<Integer> set = foll.get(followerId);
  91. if (set.contains(followeeId)) {
  92. set.remove(followeeId);
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * Your Twitter object will be instantiated and called as such:
  99. * Twitter obj = new Twitter();
  100. * obj.postTweet(userId,tweetId);
  101. * List<Integer> param_2 = obj.getNewsFeed(userId);
  102. * obj.follow(followerId,followeeId);
  103. * obj.unfollow(followerId,followeeId);
  104. */

发表评论

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

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

相关阅读

    相关 每日一题-设计

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

    相关 355. 设计

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

    相关 leetcode355 设计

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

    相关 Twitter高级搜索

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