355-Design Twitter
来源:互联网 发布:怎么安装spss13软件 编辑:程序博客网 时间:2024/04/30 22:31
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.
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
- 355-Design Twitter
- 355Design Twitter
- [leetcode-355]Design Twitter(java)
- Design Twitter
- Design Twitter
- Design Twitter
- Design Twitter
- Design Twitter
- Design Twitter
- Design Twitter
- 【Leetcode】Design Twitter
- [leetcode] 355. Design Twitter
- 【LeetCode】355. Design Twitter
- [LeetCode] Design Twitter
- leetcode_355 Design Twitter
- leetcode.355. Design Twitter
- 355. Design Twitter
- 355. Design Twitter
- Codeforces Round #271 (Div. 2) F Ant colony
- StarUML软件设计建模工具破解
- 8086汇编语言 R命令更改寄存器的内容
- poj-1222 高斯消元
- python学习——分布式进程
- 355-Design Twitter
- 一个简单代码的不简单实现
- 中软实训随记
- for循环语句
- OFDM消除ICI和ISI的原理剖析
- MVC设计模式
- office文件图标显示不正常
- Activity设置成Dialog模式
- 单调栈1001 POJ2796 Feel Good