您的位置:首页 > 其它

leetcode题解-355. Design Twitter

2017-03-03 14:12 169 查看
题目: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:

1,postTweet(userId, tweetId): Compose a new tweet.

2,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.

3,follow(followerId, followeeId): Follower follows a followee.

4,unfollow(followerId, followeeId): Follower unfollows a followee.

有题目可知,目的是Tweet的简单实现,主要包含发Tweet,关注别人,取关别人,获取最近十条“朋友圈”。可以观察到,其实前三条功能都可以很方便的实现,重点在第四条如何刷新最近十条朋友圈。我们这里使用Map来保存用户及其关注列表,同样使用map来保存用户所发的Tweet列表。并用队列实现取前十条朋友圈的功能。并定义一个Tweet类,来保存Tweet发布时间和内容。代码入下:

public class Twitter {
//key是用户id,value是用户关注列表,用集合可以去重,注意这里要把自己也加入该集合中,方便获取朋友圈时把自己的也显示出来。
Map<Integer, Set<Integer>> user;
//key是用户id, value是用户所发Tweet链表,后发的插入在最前面即可。
Map<Integer, LinkedList<Tweet>> post;
//记录所有发过的Tweet数量
int count;

//Tweet类,记录Tweet发布时间和内容
class Tweet{
int time;
int id;

Tweet(int time, int id){
this.time = time;
this.id = id;
}
}

/** Initialize your data structure here. */
public Twitter() {
user = new HashMap<>();
post = new HashMap<>();
count = 0;
}

/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
if(!user.containsKey(userId)) user.put(userId, new HashSet<>());
//把自己加入到关注列表中。
user.get(userId).add(userId);
if(!post.containsKey(userId)) post.put(userId, new LinkedList<>());
//吧tweetId加入到该用户的Tweet列表中,注意这里使用addFirst函数。
post.get(userId).addFirst(new Tweet(count++, 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) {
//如果该用户不存在返回空链表
if(!user.containsKey(userId)) return new LinkedList<Integer>();
//优先级队列。插入时会将最新发布的tweet放在最前面。
PriorityQueue<Tweet> feed = new PriorityQueue<>((t1, t2) -> t2.time-t1.time);
//这句代码实现了本函数的所有功能,非常简洁,但是却实现了复杂的逻辑关系,可以好好研究一下。
//首先filter过滤掉关注列表中没有发过Tweet的人,然后对于其他每个发过Tweet的用户,得到其Tweet列表并将其每个Tweet加入到feed队列中。
user.get(userId).stream().filter(f -> post.containsKey(f)).forEach(f -> post.get(f).forEach(feed::add));
List<Integer> res = new LinkedList<>();
while(feed.size()>0 && res.size()<10) res.add(feed.poll().id);
return res;
}

/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if(!user.containsKey(followerId)) user.put(followerId, new HashSet<>());
//添加关注
user.get(followerId).add(followeeId);
}

/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
//取关
if(user.containsKey(followerId) && followeeId != followerId) user.get(followerId).remove(followeeId);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: