355-Design Twitter
2016-07-17 16:17
225 查看
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.
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
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.
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
相关文章推荐
- 二叉树的深度 java
- 内部类
- JZOJ 4647 寻找 【NOIP2016提高A组模拟7.17】
- 游泳
- Hadoop&Spark解决二次排序问题(Hadoop篇)
- Python 小甲鱼教程 课后练习33 异常处理
- 一个简单代码的不简单实现
- 安卓学习笔记---Android APP内存优化之图片优化
- hdu-1559 最大子矩阵
- 直接,间接,立即三种寻址方式指令的执行速度,由快至慢的排序是____
- HashSet类源码解析
- 字符串处理小结
- asp、aspx、php、jsp万能密码大全
- 辗转相除法和更相减损术原理分别是什么
- IDC的相关介绍
- 7.17C组一套题的总结&题解
- iOS开发 一些常用的正则表达式工具类
- 《分布式服务框架原理与实践》——第5章协议栈阅读笔记
- poj1042 Gone Fishing
- 8086汇编语言自学经验分享 D命令查看指定地址及其之后的128字节的内容