题目链接
https://leetcode-cn.com/problems/design-twitter/
描述
设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。
你的设计需要支持以下的几个功能:
postTweet(userId, tweetId): 创建一条新的推文
getNewsFeed(userId): 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。推文必须按照时间顺序由
最近的开始排序。
follow(followerId, followeeId): 关注一个用户
unfollow(followerId, followeeId): 取消关注一个用户
示例
Twitter twitter = new Twitter();
// 用户1发送了一条新推文 (用户id = 1, 推文id = 5).
twitter.postTweet(1, 5);
// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
twitter.getNewsFeed(1);
// 用户1关注了用户2.
twitter.follow(1, 2);
// 用户2发送了一个新推文 (推文id = 6).
twitter.postTweet(2, 6);
// 用户1的获取推文应当返回一个列表,其中包含两个推文,id分别为 -> [6, 5].
// 推文id6应当在推文id5之前,因为它是在5之后发送的.
twitter.getNewsFeed(1);
// 用户1取消关注了用户2.
twitter.unfollow(1, 2);
// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
// 因为用户1已经不再关注用户2.
twitter.getNewsFeed(1);
初始代码模板
class Twitter {
/** Initialize your data structure here. */
public Twitter() {
}
/** Compose a new tweet. */
public void postTweet(int userId, int 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. */
public List<Integer> getNewsFeed(int userId) {
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
}
}
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* List<Integer> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/
代码
代码看起来有些复杂,毕竟设计题需要考虑很多条件。不过看下面这篇题解就好了,大佬写的很棒:
https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/2.4-shou-ba-shou-she-ji-shu-ju-jie-gou/she-ji-twitter
class Twitter {
//时间戳,用来区分发推特的时间
private int timestamp;
//用户对象,保存关注列表,以及发帖
private class User {
int userId;
Set<Integer> followSet;
Tweet first;
User(int userId) {
this.userId = userId;
this.followSet = new HashSet<>();
followSet.add(userId);
this.first = null;
}
void addFollow(int userId) {
this.followSet.add(userId);
}
void removeFollow(int userId) {
if (this.userId != userId) followSet.remove(userId);
}
void postTweet(int tweetId, int time) {
Tweet tweet = new Tweet(tweetId, time);
tweet.next = first;
first = tweet;
}
}
//推特对象,由于需要时间作为区分,所以封装了一下
private class Tweet {
int tweetId;
int time;
Tweet next;
Tweet(int tweetId, int time) {
this.tweetId = tweetId;
this.time = time;
this.next = null;
}
}
//把用户的ID和用户对象关联起来
private Map<Integer, User> map;
/** Initialize your data structure here. */
public Twitter() {
this.timestamp = 0;
this.map = new HashMap<>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
if (!map.containsKey(userId)) {
map.put(userId, new User(userId));
}
map.get(userId).postTweet(tweetId, timestamp);
timestamp++;
}
/** 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. */
public List<Integer> getNewsFeed(int userId) {
List<Integer> list = new LinkedList<>();
if (!map.containsKey(userId)) {
return list;
}
PriorityQueue<Tweet> queue = new PriorityQueue<>((x, y) -> y.time - x.time);
for (int user : map.get(userId).followSet) {
if (map.get(user).first == null) continue;
queue.offer(map.get(user).first);
}
while (!queue.isEmpty()) {
if (list.size() == 10) {
break;
}
Tweet cur = queue.poll();
list.add(cur.tweetId);
cur = cur.next;
if (cur != null) {
queue.offer(cur);
}
}
return list;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if (!map.containsKey(followerId)) {
map.put(followerId, new User(followerId));
}
if (!map.containsKey(followeeId)) {
map.put(followeeId, new User(followeeId));
}
map.get(followerId).addFollow(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if (map.containsKey(followerId)) {
map.get(followerId).removeFollow(followeeId);
}
}
}
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* List<Integer> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/