Leetcode 355. Design Twitter 实现一个Twitter系统 解题报告

1 解题报告

首先,手受伤了,不太方便打字,少打一些

这道题是说设计一个简单的推特系统,能够发推特,关注人,取消关注人,以及获取最近10条消息,这里我说下每个点的要点:

1、发推特:需要记录一个时间戳,为了维护方便,这里我用了自定义的一个类来表示一条推特,不然数据不容易看,也不容易计算。。

2、关注人:小心重复关注,所以一定要使用HashSet或HashMap,当然如果你用List那就要不断检查重复了,不值得。。其次,他可能会有关注自己的动作出现,这个处理好,否则检索的时候自己的推特可能会出现两次

3、取消关注:如果你关注上面用了这个,那么这个简单,就是小心本来没有关注的就好

4、拉取动态:包含自己和关注人在内的最近10条微博,这个要注意自己的有没有关注自己(最好就是不要增加自己关注自己的情况),从自己和所有关注的人拉取前10条在一起,然后排序输出就好

2 原题

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);

3 AC解

public class Twitter {
    /**
     * 需要定义一个表示每一条状态的,主要是时间id,以及重新定义排序的
     * 
     * */
    class Tweet {
        public int time;
        public int tweetId;
        public Tweet(int tweetId,int time){
            this.time = time;
            this.tweetId = tweetId;
        }

    }
    int timeStamp ;
    //每个人发布的推特信息
    HashMap<Integer,List<Tweet>> timelines;
    //人际关系,会重复follow所以要用set
    HashMap<Integer,HashSet<Integer>> relations;
    /** Initialize your data structure here. */
    public Twitter() {
        this.timelines = new HashMap<Integer,List<Tweet>>();
        this.relations = new HashMap<Integer,HashSet<Integer>>();

    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if(timelines.containsKey(userId) == false){
            timelines.put(userId, new ArrayList<Tweet>());
        }
        timelines.get(userId).add(new Tweet(tweetId,timeStamp++));

    }

    /** 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. */
    /**
     * 全部都选前10,然后按照时间戳排序,注意要选择自己的和别人的
     * */
    public List<Integer> getNewsFeed(int userId) {
        HashSet<Integer> followees = relations.get(userId);
        List<Tweet> candidates = new ArrayList<Tweet>();
        //分别选择,可以选择每个人的前10条就好
         List<Tweet> timeline = timelines.get(userId);
         if(timeline!=null){
             for(int i=timeline.size()-1;i>=Math.max(0,timeline.size()-10);i--){
                    candidates.add(timeline.get(i));
                }
         }
        if(followees != null){ 
            for(Integer followee:followees){
                 timeline = timelines.get(followee);
                 if(timeline == null)
                    continue;
                 for(int i=timeline.size()-1;i>=Math.max(0,timeline.size()-10);i--){
                    candidates.add(timeline.get(i));
                }
            }
        }
        Collections.sort(candidates,new Comparator<Tweet> (){
             public int compare(Tweet o1, Tweet o2) {
                 return o2.time - o1.time;
                }
            });
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<Math.min(10,candidates.size());i++){
            list.add(candidates.get(i).tweetId);
        }
        return list;
    }

    /** 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;
        if(relations.containsKey(followerId)==false){
            relations.put(followerId,new HashSet<Integer>());
        }
        relations.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) {
        HashSet<Integer> list = relations.get(followerId);
        if(list == null) return ;
        list.remove(followeeId);
    }
}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值