合并排序数组 II
题目
实现一个迷你的推特,支持下列几种方法
1.postTweet(user_id, tweet_text). 发布一条推特.
2.getTimeline(user_id). 获得给定用户最新发布的十条推特,按照发布时间从最近的到之前排序
3.getNewsFeed(user_id). 获得给定用户的朋友或者他自己发布的最新十条推特,从发布时间最近到之前排序
4.follow(from_user_id, to_user_id). from_user_id 关注 to_user_id.
5.unfollow(from_user_id, to_user_id). from_user_id 取消关注 to_user_id.样例
题解
先对题目进行一下说明:
1.postTweet(user_id, tweet_text).
将用户发送的tweet存入数据库。
2.getTimeline(user_id).
拉取指定用户发送的最近10条消息。
3.getNewsFeed(user_id).
拉取与指定用户相关的最近10条消息,包括用户自己发送的和该用户关注的用户发送的。
4.follow(from_user_id, to_user_id). from_user_id 关注 to_user_id.
将 to_user_id加入指定用户的关注列表。
5.unfollow(from_user_id, to_user_id). from_user_id 取消关注 to_user_id.
将 to_user_id从指定用户的关注列表中删除。
使用的数据结构:
Tweet数据库使用线性结构,以保证时间线性。关注列表由于无关乎顺序,顾采用HashMap中嵌套HashSet的结构以降低时间复杂度。
/**
* Definition of Tweet:
* public class Tweet {
* public int id;
* public int user_id;
* public String text;
* public static Tweet create(int user_id, String tweet_text) {
* // This will create a new tweet object,
* // and auto fill id
* }
* }
*/
public class MiniTwitter {
private List<Tweet> tweetList = new ArrayList<Tweet>();
private HashMap<Integer,HashSet<Integer>> followList = new HashMap<>();
public MiniTwitter() {
// initialize your data structure here.
}
// @param user_id an integer
// @param tweet a string
// return a tweet
public Tweet postTweet(int user_id, String tweet_text) {
Tweet t = Tweet.create(user_id,tweet_text);
tweetList.add(t);
return t;
}
// @param user_id an integer
// return a list of 10 new feeds recently
// and sort by timeline
public List<Tweet> getNewsFeed(int user_id) {
List<Tweet> newsFeed = new ArrayList<Tweet>();
HashSet<Integer> follows = followList.get(user_id);
for (int i=tweetList.size()-1,count=0;i>=0 && count < 10;i--)
{
if (tweetList.get(i).user_id == user_id || (follows != null && follows.contains(tweetList.get(i).user_id)))
{
count++;
newsFeed.add(tweetList.get(i));
}
}
return newsFeed;
}
// @param user_id an integer
// return a list of 10 new posts recently
// and sort by timeline
public List<Tweet> getTimeline(int user_id) {
List<Tweet> timeline = new ArrayList<Tweet>();
for (int i=tweetList.size()-1,count=0;i>=0 && count < 10;i--)
{
if (tweetList.get(i).user_id == user_id)
{
count++;
timeline.add(tweetList.get(i));
}
}
return timeline;
}
// @param from_user_id an integer
// @param to_user_id an integer
// from user_id follows to_user_id
public void follow(int from_user_id, int to_user_id) {
if (followList.get(from_user_id) == null)
{
followList.put(from_user_id,new HashSet<Integer>());
}
HashSet<Integer> follows = followList.get(from_user_id);
if (!follows.contains(to_user_id))
{
follows.add(to_user_id);
}
}
// @param from_user_id an integer
// @param to_user_id an integer
// from user_id unfollows to_user_id
public void unfollow(int from_user_id, int to_user_id) {
if (followList.get(from_user_id) == null)
{
return;
}
HashSet<Integer> follows = followList.get(from_user_id);
if (follows.contains(to_user_id))
{
follows.remove(to_user_id);
}
}
}
Last Update 2016.9.23