501.Mini Twitter-迷你推特(中等题)

合并排序数组 II

  1. 题目

    实现一个迷你的推特,支持下列几种方法
    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.

  2. 样例

    这里写图片描述

  3. 题解

先对题目进行一下说明:
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值