355. Design Twitter
Posted life is tough,so are you
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
- postTweet(userId, tweetId): Compose a new tweet.
- 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.
- follow(followerId, followeeId): Follower follows a followee.
- unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
Twitter twitter = new Twitter(); // User 1 posts a new tweet (id = 5). twitter.postTweet(1, 5); // User 1‘s news feed should return a list with 1 tweet id -> [5]. twitter.getNewsFeed(1); // User 1 follows user 2. twitter.follow(1, 2); // User 2 posts a new tweet (id = 6). twitter.postTweet(2, 6); // User 1‘s news feed should return a list with 2 tweet ids -> [6, 5]. // Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5. twitter.getNewsFeed(1); // User 1 unfollows user 2. twitter.unfollow(1, 2); // User 1‘s news feed should return a list with 1 tweet id -> [5], // since user 1 is no longer following user 2. twitter.getNewsFeed(1);
思路:设置一个变量count作为时间。
tweetdict【用户id,dict2】 其中dict2【时间,博文】
followdict【用户id,关注人id】
postTweet(userId, tweetId): 更新tweetdict
getNewsFeed(userId):显示自己和关注人前十条tweeter。自己的tweeter+关注人tweeter加入dict1,按时间排序,选出前十个显示。
follow(followerId, followeeId): 更新followdict
unfollow(followerId, followeeId):更新followdict
注意问题:dict元素的并不是按照添加的顺序排列的
class Twitter(object): def __init__(self): """ Initialize your data structure here. """ self.time_count = 0 self.tweetdict = {} self.followdict = {} def postTweet(self, userId, tweetId): """ Compose a new tweet. :type userId: int :type tweetId: int :rtype: void """ self.time_count += 1 if userId not in self.tweetdict.keys(): self.tweetdict[userId]={self.time_count:tweetId} else: temp_dict = self.tweetdict.get(userId) temp_dict[self.time_count] = tweetId self.tweetdict[userId] = temp_dict 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] """ dict1 = {} if userId in self.tweetdict.keys(): dict1 = self.tweetdict[userId].copy() if userId in self.followdict.keys(): followlist = self.followdict[userId] print followlist if followlist != []: for followId in followlist: if followId in self.tweetdict.keys(): dicti = self.tweetdict[followId] dict1.update(dicti) keylist = sorted(dict1.keys()) #dict1 time,tweet user and his follow l = list(reversed(keylist)) if len(l) < 10: l2 = l else: l2 = [] for i in range(10): l2.append(l[i]) rlist = [] for i in l2: rlist.append(dict1[i]) return rlist 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.followdict.keys(): #yonghu de fen si l[followee] self.followdict[followerId] = [followeeId] else: self.followdict.get(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 """ if followerId in self.followdict.keys(): if followeeId in self.followdict[followerId]: self.followdict[followerId].remove(followeeId) # Your Twitter object will be instantiated and called as such: obj = Twitter() obj.postTweet(2,5) obj.postTweet(1,3) obj.postTweet(1,101) obj.postTweet(2,13) obj.postTweet(2,10) obj.postTweet(1,2) obj.postTweet(2,94) obj.postTweet(2,50) obj.postTweet(1,333) obj.postTweet(1,22) print obj.tweetdict print obj.getNewsFeed(2) # obj.follow(1,5) # print obj.getNewsFeed(1) # obj.follow(2,1) # print obj.getNewsFeed(2) # obj.unfollow(2,1) # print obj.getNewsFeed(2) # for i in range(7): # obj.postTweet(1,1) # print obj.tweetdict # obj.postTweet(2,2) # print obj.tweetdict # obj.postTweet(3,3) # print obj.tweetdict # obj.postTweet(4,4) # print obj.tweetdict # for i in range(7): # obj.postTweet(1,1) # # print getNewsFeed(1) # obj.follow(1,2) # obj.follow(1,3) # print obj.getNewsFeed(1) # obj.unfollow(1,3) # print obj.getNewsFeed(1)
以上是关于355. Design Twitter的主要内容,如果未能解决你的问题,请参考以下文章
leetcode@ [355] Design Twitter (Object Oriented Programming)