注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
最近看了这个leetcode题 然后有个人post 一个答案
- class Twitter(object):
- def __init__(self):
- """
- Initialize your data structure here.
- """
- self.tweets = collections.defaultdict(list)
- self.following = collections.defaultdict(set)
- self.order = 0
-
- def postTweet(self, userId, tweetId):
- """
- Compose a new tweet.
- :type userId: int
- :type tweetId: int
- :rtype: None
- """
- self.tweets[userId] += (self.order, tweetId),
- self.order -= 1
- 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]
- """
- tw = sorted(tw for i in self.following[userId] | {userId} for tw in self.tweets[i])[:10]
-
- print(tw)
- return [news for i, news in tw]
-
- 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: None
- """
- self.following[followerId].add(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: None
- """
- self.following[followerId].discard(followeeId)
复制代码
请问 tw = sorted(tw for i in self.following[userId] | {userId} for tw in self.tweets[i])[:10] 这行是怎么回事 谢谢!!!
|