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);
这个题我先想复杂了,我先想得是设计三个map,
Map<Integer, Set<Integer>> followerMap;
Map<Integer, Set<Integer>> followeeMap;
Map<Integer, List<Post>> postMap;
将所有的信息先安排好,发布推特的时候在别人的圈子里也加入信息。这样太麻烦了!
其实只要在最后读推特的时候在关注对象的列表中读出他们发布的推特就行了!
这里要注意的是检查是否包含!
public class Twitter {
class TwitterNode{
int userId;
int twitterId;
TwitterNode(int userId,int twitterId){
this.userId = userId;
this.twitterId = twitterId;
}
}
List<TwitterNode> twitterList;
Map<Integer,Set> userManager;
/** Initialize your data structure here. */
public Twitter() {
twitterList = new LinkedList<TwitterNode>();
userManager = new HashMap<Integer,Set>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
if(!userManager.containsKey(userId)){
Set<Integer> tmp = new HashSet<>();
tmp.add(userId);
userManager.put(userId,tmp);
}
TwitterNode node = new TwitterNode(userId,tweetId);
twitterList.add(0,node);
}
/** 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> res = new LinkedList<>();
if(userManager.containsKey(userId)){
Set<Integer> follows = userManager.get(userId);
int count = 0;
for(TwitterNode curNode:twitterList){
if(count == 10)
break;
if(follows.contains(curNode.userId)){
res.add(curNode.twitterId);
count++;
}
}
}
return res;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
Set<Integer> focusSet;
if(!userManager.containsKey(followeeId)){//必须包含被关注者
focusSet = new HashSet<>();
focusSet.add(followeeId);
userManager.put(followeeId,focusSet);
}
if(!userManager.containsKey(followerId)){
focusSet = new HashSet<>();
focusSet.add(followerId);
userManager.put(followerId,focusSet);
}
userManager.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(followerId != followeeId && userManager.containsKey(followerId)){
if(userManager.containsKey(followeeId)){
userManager.get(followerId).remove(followeeId);
}
}
}
}