设计一个简化版的推特(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);
题解:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
public class Twitter {
/**
* 用户 id 和推文(单链表)的对应关系
*/
private Map<Integer, Tweet> twitter;
/**
* 用户 id 和他关注的用户列表的对应关系
*/
private Map<Integer, Set<Integer>> followings;
/**
* 全局使用的时间戳字段,用户每发布一条推文之前 + 1
*/
private static int timestamp = 0;
/**
* 合并 k 组推文使用的数据结构(可以在方法里创建使用),声明成全局变量非必需,视个人情况使用
*/
private static PriorityQueue<Tweet> maxHeap;
/**
* Initialize your data structure here.
*/
public Twitter() {
//初始化 关注列表
followings = new HashMap<>();
//初始化 自己发的推特
twitter = new HashMap<>();
//初始化 要展示的列表
maxHeap = new PriorityQueue<>((o1, o2) -> -o1.timestamp + o2.timestamp);
}
/**
* Compose a new tweet.
*/
//发推特
public void postTweet(int userId, int tweetId) {
//时间戳+1
timestamp++;
//如果发的列表里面 有userid,那么,旧的节点指向现在这个点twitter,,新的节点创建这个要发布的节点,并且新的节点的next就是旧的节点。
//然后将新的节点放入这个HashMap中
//简短的来说,就是twitter这个HashMap存放的是Tweet,而Tweet是一个单向链表,只需要修改twitter中这个user的最新Tweet就好了。
if (twitter.containsKey(userId)) {
Tweet oldHead = twitter.get(userId);
Tweet newHead = new Tweet(tweetId, timestamp);
newHead.next = oldHead;
twitter.put(userId, newHead);
} else {//如果不存在这个userId则直接加入就好
twitter.put(userId, new Tweet(tweetId, 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.
*/
//获取推特列表最多10条
public List<Integer> getNewsFeed(int userId) {
// 由于是全局使用的,使用之前需要清空
maxHeap.clear();
// 如果自己发了推文也要算上
if (twitter.containsKey(userId)) {
maxHeap.offer(twitter.get(userId));
}
//关注人的推文也要算上
Set<Integer> followingList = followings.get(userId);
if (followingList != null && followingList.size() > 0) {
for (Integer followingId : followingList) {
//获取关注人的推特列表
Tweet tweet = twitter.get(followingId);
if (tweet != null) {
maxHeap.offer(tweet);
}
}
}
//十条结果
List<Integer> res = new ArrayList<>(10);
int count = 0;
while (!maxHeap.isEmpty() && count < 10) {
Tweet head = maxHeap.poll();
res.add(head.id);
// 这里最好的操作应该是 replace,但是 Java 没有提供
if (head.next != null) {
maxHeap.offer(head.next);
}
count++;
}
return res;
}
/**
* Follower follows a followee. If the operation is invalid, it should be a no-op.
*
* @param followerId 发起关注者 id
* @param followeeId 被关注者 id
*/
//关注人
public void follow(int followerId, int followeeId) {
// 被关注人不能是自己
if (followeeId == followerId) {
return;
}
// 获取我自己的关注列表
Set<Integer> followingList = followings.get(followerId);
//关注列表是空的,没关注过别人,那么需要初始化一下,添加一个Set<Integer>并把这次要关注的人放进去
if (followingList == null) {
Set<Integer> init = new HashSet<>();
init.add(followeeId);
followings.put(followerId, init);
} else {//关注列表不是空的
//但是如果已经关注过这个人了,那么就返回空,否则添加关注
if (followingList.contains(followeeId)) {
return;
}
followingList.add(followeeId);
}
}
/**
* Follower unfollows a followee. If the operation is invalid, it should be a no-op.
*
* @param followerId 发起取消关注的人的 id
* @param followeeId 被取消关注的人的 id
*/
//取消关注
public void unfollow(int followerId, int followeeId) {
//不能取消对自己的关注
if (followeeId == followerId) {
return;
}
// 获取我自己的关注列表
Set<Integer> followingList = followings.get(followerId);
//如果关注列表是空的,肯定取消不了,返回空
if (followingList == null) {
return;
}
// 这里删除之前无需做判断,因为查找是否存在以后,就可以删除,反正删除之前都要查找
followingList.remove(followeeId);
}
/**
* 推文类,是一个单链表(结点视角)
*/
private class Tweet {
/**
* 推文 id
*/
private int id;
/**
* 发推文的时间戳
*/
private int timestamp;
private Tweet next;
public Tweet(int id, int timestamp) {
this.id = id;
this.timestamp = timestamp;
}
}
//下面这些可以删掉
public static void main(String[] args) {
Twitter twitter = new Twitter();
twitter.postTweet(1, 1);
List<Integer> res1 = twitter.getNewsFeed(1);
System.out.println(res1);
twitter.follow(2, 1);
List<Integer> res2 = twitter.getNewsFeed(2);
System.out.println(res2);
twitter.unfollow(2, 1);
List<Integer> res3 = twitter.getNewsFeed(2);
System.out.println(res3);
}
}
这里面最重要的还是PriorityQueue这个数据结构的使用!
可以将多个链表,按照一定的顺序整理成一个链表。
然后这个多看,多看!!!一定要回来再看!
参考链接🔗:https://leetcode-cn.com/problems/design-twitter/solution/ha-xi-biao-lian-biao-you-xian-dui-lie-java-by-liwe/