【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/design-twitter/description/
题目描述
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);
题目大意
产生一个推特系统,这个推特能关注、发推,解关注,获得信息流。
解题方法
这个题主要是考察自己的工程实践能力。这一块,使用了优先级队列。在Python中,优先级队列其实就是可以使用heapq模块实现。
我提交失误的地方主要在于,取消关注的时候,需要多进行判断,是否存在这个用户、被关注的用户,以及他们之间是否存在关注关系等等。
这些判断在其他的函数中并没有使用到,所以注意细节。
另外,下面的做法好像很复杂,足足有100行。其实可以使用defaultdict等数据结构优化代码,使用排序代替heapq的。按下不表。
代码如下:
class User(object):
"""
User structure
"""
def __init__(self, userId):
self.userId = userId
self.tweets = set()
self.following = set()
class Tweet(object):
"""
Tweet structure
"""
def __init__(self, tweetId, userId, ts):
self.tweetId = tweetId
self.userId = userId
self.ts = ts
def __cmp__(self, other):
#call global(builtin) function cmp for int
return cmp(other.ts, self.ts)
class Twitter(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.ts = 0
self.userMap = dict()
def postTweet(self, userId, tweetId):
"""
Compose a new tweet.
:type userId: int
:type tweetId: int
:rtype: void
"""
if userId not in self.userMap:
self.userMap[userId] = User(userId)
tweet = Tweet(tweetId, userId, self.ts)
self.userMap[userId].tweets.add(tweet)
self.ts += 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]
"""
res = list()
que = []
if userId not in self.userMap:
return res
mainUser = self.userMap[userId]
for t in mainUser.tweets:
heapq.heappush(que, t)
for u in mainUser.following:
for t in u.tweets:
heapq.heappush(que, t)
n = 0
while que and n < 10:
res.append(heapq.heappop(que).tweetId)
n += 1
return res
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 followeeId not in self.userMap:
self.userMap[followeeId] = User(followeeId)
if followerId not in self.userMap:
self.userMap[followerId] = User(followerId)
if followerId == followeeId:
return
followee = self.userMap[followeeId]
self.userMap[followerId].following.add(followee)
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 == followeeId) or (followerId not in self.userMap) or (followeeId not in self.userMap):
return
followee = self.userMap[followeeId]
if followee in self.userMap.get(followerId).following:
self.userMap.get(followerId).following.remove(followee)
# Your Twitter object will be instantiated and called as such:
# obj = Twitter()
# obj.postTweet(userId,tweetId)
# param_2 = obj.getNewsFeed(userId)
# obj.follow(followerId,followeeId)
# obj.unfollow(followerId,followeeId)
C++版本的代码如下:
class Twitter {
public:
/** Initialize your data structure here. */
Twitter() {
cnt = 0;
}
/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
follow(userId, userId);
tweets[userId].push_back({cnt++, tweetId});
}
/** 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. */
vector<int> getNewsFeed(int userId) {
vector<int> res;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;
for (auto &it : friends[userId]) {
for (auto &a : tweets[it]) {
if (!q.empty() && q.top().first > a.first && q.size() > 10) break;
q.push(a);
if (q.size() > 10) q.pop();
}
}
while (!q.empty()) {
res.push_back(q.top().second);
q.pop();
}
reverse(res.begin(), res.end());
return res;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
friends[followerId].insert(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
if (followeeId != followerId)
friends[followerId].erase(followeeId);
}
private:
int cnt;
unordered_map<int, set<int>> friends;
unordered_map<int, vector<pair<int, int>>> tweets;
};
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* vector<int> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/
参考资料:
日期
2018 年 8 月 28 日 —— 雾霾天
2018 年 12 月 6 日 —— 周四啦!
【LeetCode】355. Design Twitter 解题报告(Python & C++)的更多相关文章
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- LeetCode 705 Design HashSet 解题报告
题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...
- LeetCode 706 Design HashMap 解题报告
题目要求 Design a HashMap without using any built-in hash table libraries. To be specific, your design s ...
- [leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- 完全用Deepin Linux娱乐、工作、学习(1)
截至今天我已经用全Deepin Desktop Linux环境娱乐.工作.学习了100多天.当你看到这个桌面的时候,会不会觉得它是MacOS?错了,它是Deepin Desktop Linux,而且它 ...
- 安装octave详解
1. 一些可以替换的库(可跳过) 默认的库安装libblas.dll.OpenBLAS-v2.6.0-0-54e7b37_dynamicarch_nt4(自动检测CPU类型) 在目录下<your ...
- 在 vscode.dev 中直接运行 Python !纯浏览器环境,无后端!
其实有挺长一段时间没有写自己的 VS Code 插件了! 还是要感谢我们 DevDiv 组的 Flexible Friday 活动,让我可以在工作日研究自己感兴趣的项目. Flexible Frida ...
- 日常Java 2021/10/28
Java lterator Java lterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayList和HashSet等集合.lterator是Java迭代器最简单的 ...
- 基于Annotation(注解)的装配
一.常用注解 1.@Component 是一种通用注解,可用于任何Bean 2.@Repository 通常用于注解DAO层类,即持久层 3.@Service 通常用于注解Service类,即服务层 ...
- 聊聊 SpringBoot 中的两种占位符:@*@ 和 ${*}
前言 在 SpringBoot 项目中,我们经常会使用两种占位符(有时候还会混用),它们分别是: @*@ ${*} 如果我们上网搜索「SpringBoot 的占位符 @」,大部分答案会告诉你,Spri ...
- Jenkins安全加固
1.jenkins未授权访问 描述 jenkins不当配置可导致未授权访问管理控制台,可以通过脚本命令行执行系统命令.通过该漏洞,可以后台管理服务,通过脚本命令行功能执行系统命令,如反弹shell,w ...
- Java后端高频知识点学习笔记1---Java基础
Java后端高频知识点学习笔记1---Java基础 参考地址:牛_客_网 https://www.nowcoder.com/discuss/819297 1.重载和重写的区别 重载:同一类中多个同名方 ...
- [BUUCTF]PWN——[BJDCTF 2nd]ydsneedgirlfriend2
[BJDCTF 2nd]ydsneedgirlfriend2 附件 步骤: 例行检查,64位程序,开启了canary和nx 试运行一下程序,看看大概的情况,经典的堆块的布局 64位ida载入,习惯性的 ...
- android 基于dex的插件化开发
安卓里边可以用DexClassLoader实现动态加载dex文件,通过访问dex文件访问dex中封装的方法,如果dex文件本身还调用了native方法,也就间接实现了runtime调用native方法 ...