355-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.
Idea:此题的难点在于(2)根据userId得到该user或者该user follow的user所发的最近的10条twitter。在这里我用post_twitter=list([userId, twitterId])的形式来存储整个twitter系统中user所发的twitter,在getNewsFeed()中就要将post_twitter做一个逆序保证从左到右是按照由近到远的时间顺序。 但是post_twitter在getNewsFeed中是不能够改变的,因此要将它的拷贝对象做一个逆序,不能使用等号来浅拷贝因为这样对复制对象的改变post_twitter也变了,要用temp_twitter = [i for i in self.post_twieets]这样的方式来复制.

code:

class Twitter(object):
 
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.user_follow = {}
        self.post_twieets = []
 
    def postTweet(self, userId, tweetId):
        """
        Compose a new tweet.
        :type userId: int
        :type tweetId: int
        :rtype: void
        """
        item = [userId, tweetId]
        self.post_twieets.append(item)
                 
         
 
    def getNewsFeed(self, 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.
        :type userId: int
        :rtype: List[int]
        """
        twitters_10 = []
        temp_twitter = [i for i in self.post_twieets]
        
        temp_twitter.reverse()
        for item in temp_twitter:
            if item[0] == userId:
                twitters_10.append(item[1])
            else:
                if userId in self.user_follow:
                    if item[0] in self.user_follow[userId]:
                        twitters_10.append(item[1])
                         
            if len(twitters_10) == 10:
                break
             
        return twitters_10            
 
    def follow(self, followerId, followeeId):
        """
        Follower follows a followee. If the operation is invalid, it should be a no-op.
        :type followerId: int
        :type followeeId: int
        :rtype: void
        """
        if followerId not in self.user_follow:
            self.user_follow[followerId] = []
        self.user_follow[followerId].append(followeeId)
 
    def unfollow(self, followerId, followeeId):
        """
        Follower unfollows a followee. If the operation is invalid, it should be a no-op.
        :type followerId: int
        :type followeeId: int
        :rtype: void
        """
        try:
            self.user_follow[followerId].remove(followeeId)
        except: pass
         


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值