设计推特
题目
设计一个简化版的推特(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);
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-twitter
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
简单的会想到这是一个全局会使用的系统.所以需要map来存储与用户有关的关系列表,与用户有关的推特列表结构.而又因为推特是根据时间线性排列的,所以使用链表即可.那最难的地方在于取推特的功能.相应的就是一个合并K个排序链表的问题.
代码上参考了大神的解答.
代码
public class Twitter {
//全局考量,需要有两个map.一个是存储用户关系,一个是存储用户的twitter列表.
private Map<Integer,Tweet> twitter;
private Map<Integer, Set<Integer>> followings;
/**
* 全局时间戳,每位用户发一条twitter时加一,用户排序
*/
private static int timeStamp = 0;
public Twitter() {
twitter = new HashMap<Integer, Tweet>();
followings = new HashMap<Integer, Set<Integer>>();
}
void postTweet(int userId,int tweetId){
timeStamp++;
if(twitter.containsKey(userId)){
Tweet old = twitter.get(userId);
Tweet newTwitter = new Tweet(tweetId, timeStamp);
newTwitter.next = old;
twitter.put(userId,newTwitter);
}else{
twitter.put(userId,new Tweet(tweetId,timeStamp));
}
}
public List<Integer> getNewsFeed(int userId){
PriorityQueue<Tweet> maxheap = new PriorityQueue<>((o1,o2) -> -o1.timeStamp+o2.timeStamp);
//先获取自己的推文
if(twitter.get(userId) != null){
maxheap.offer(twitter.get(userId));
}
//获取关注的人的推文
Set<Integer> followers = followings.get(userId);
if(followers != null && !followers.isEmpty()){
for (Integer follower : followers) {
Tweet tweet = twitter.get(follower);
if (tweet != null){
maxheap.offer(tweet);
}
}
}
//从maxheap中获取头节点,然后将头结点后面的节点入队.再次查找.
List<Integer> result =new ArrayList<>();
int count = 0;
while (!maxheap.isEmpty() && count < 10){
Tweet head = maxheap.poll();
result.add(head.id);
if(head.next != null){
maxheap.offer(head.next);
}
count++;
}
return result;
}
public void follow(int followerId,int followeeId){
if (followeeId == followerId){
return ;
}
Set<Integer> followingList = followings.get(followerId);
if (followingList == null){
followingList = new HashSet<>();
followingList.add(followeeId);
followings.put(followerId,followingList);
}else{
if (followingList.contains(followeeId)){
return;
}else{
followingList.add(followeeId);
}
}
}
public void unfollow(int followerId,int followeeId){
if (followeeId == followerId){
return ;
}
Set<Integer> followingList = followings.get(followerId);
if (followingList == null){
return;
}
followingList.remove(followeeId);
}
class Tweet{
private int id;
private int timeStamp;
private Tweet next;
public Tweet(int id,int timeStamp){
this.id = id;
this.timeStamp = timeStamp;
}
}
}
网友评论