题目描述:
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);
其实就是构造一个数据结构,总的来说只需要保存好1. user 对 followee的对应关系; 2. user与自己posts的对应关系 就行了。
所以首选HashMap。
但是注意的是,getNewsFeed的要求是返回最近的10个posts, 所以每个posts需要有一个记录时间的变量,这里没必要再为post建立一个新类,只要用给Twitter加一个变量 timeline保存posts的发布时间即可,每发布一个posts ,timeline加1,一起保存在user_posts 的HashMap里。
下面是代码:
public class Twitter {
/** Initialize your data structure here. */
HashMap<Integer,HashMap<Integer,Integer>>user_postMap ;
HashMap<Integer,HashSet<Integer>>user_followeeMap;
int timeline;
public Twitter() {
user_postMap=new HashMap<Integer,HashMap<Integer,Integer>>();
user_followeeMap=new HashMap<Integer,HashSet<Integer>>();
timeline=0;
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
HashMap<Integer,Integer>m;
if(user_postMap.containsKey(userId))
m=user_postMap.get(userId);
else
m=new HashMap<Integer,Integer>();
m.put(tweetId, timeline);
user_postMap.put(userId, m);
timeline++;
}
/** 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>l=new ArrayList<Integer>();
List<int[]>l_pair=new ArrayList<int[]>();
HashSet <Integer>followeeSet=user_followeeMap.get(userId);
int[]tweetID_timelinePair;
if(user_postMap.containsKey(userId)){
for(int tweetID:user_postMap.get(userId).keySet()){
tweetID_timelinePair=new int[2];
tweetID_timelinePair[0]=tweetID;
tweetID_timelinePair[1]=user_postMap.get(userId).get(tweetID);
l_pair.add(tweetID_timelinePair);
}}
if(user_followeeMap.containsKey(userId)){
for(int foloweeID:followeeSet){
if(!user_postMap.containsKey(foloweeID))continue;
// System.out.println("foloweeID:"+foloweeID);
for(int tweetID:user_postMap.get(foloweeID).keySet()){
tweetID_timelinePair=new int[2];
tweetID_timelinePair[0]=tweetID;
tweetID_timelinePair[1]=user_postMap.get(foloweeID).get(tweetID);
l_pair.add(tweetID_timelinePair);
}
}}
Collections.sort(l_pair,new PairComp());
int maxIdx;
if(l_pair.size()<10)maxIdx=l_pair.size();
else maxIdx=10;
for(int i=0;i<maxIdx;i++){
l.add(l_pair.get(i)[0]);
}
return l;
}
class PairComp implements Comparator<int[]>{
@Override
public int compare(int[] e1, int[] e2) {
if(e1[1] < e2[1]){
return 1;
} else {
return -1;
}
}
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if(followerId==followeeId)return;
HashSet <Integer>followeeSet;
if (user_followeeMap.containsKey(followerId)){
followeeSet=user_followeeMap.get(followerId);
}else
followeeSet=new HashSet<Integer>();
followeeSet.add(followeeId);
user_followeeMap.put(followerId, followeeSet);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if(user_followeeMap.containsKey(followerId)){
HashSet<Integer>followeeSet=user_followeeMap.get(followerId);
if(followeeSet.contains(followeeId)){
followeeSet.remove(followeeId);
user_followeeMap.put(followerId, followeeSet);
}
}
}}