leetcode Design Twitter

leetcode 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. Your design should support the following methods:

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. getNewsFeed(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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. unfollow(followerId, followeeId): Follower unfollows a followee.

Example:

题目地址:leetcode Design Twitter

题意:要求设计一个数据结构,使其能满足twitter的4种基本操作,发推、获得关注用户和自身最新10条推文、关注用户和取消关注。

思路:水题。和上一题一样,不明白为啥为hard。  可能难点就在于,直接写出无bug 的code吧。

需要那些非法的情况需要进行考虑吧。比如:

  • follow操作 followerId, followeeId 相等
  • unfollow操作followerId 不存在,或者followerId 压根就没关注 followeeId

求10个最近的推文可以用堆(当然这里我没有)

 

Python

 

本题是leetcode 355 Design Twitter 题解

更多题解可以查看:https://www.hrwhisper.me/leetcode-algorithm-solution/

 

 

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Design Twitter
本文地址:https://www.hrwhisper.me/leetcode-design-twitter/

听说长得好看的已经打赏了

Leetcode , , . permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *