leetcode [355]Design Twitter

时间:2019-06-15
本文章向大家介绍leetcode [355]Design Twitter,主要包括leetcode [355]Design Twitter使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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:

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. 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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. 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);

题目大意:

实现可以关注关注人,取消关注人,获取最新的10条消息等函数。

解法:

其实还是主要要实现两个map,一个map存放着用户id到用户关注列表的映射,一个map存放着用户id到用户所发消息的映射。order是记录消息所发的时间顺序。

java:

class Message{
    int userId;
    int twitterId;
    int order;
    public Message(int userId,int twitterId,int order){
        this.userId=userId;
        this.twitterId=twitterId;
        this.order=order;
    }
}

class Twitter {
    private static int order=0;
    private Map<Integer,Set<Message>>messages;
    private Map<Integer,Set<Integer>>followers;

    /** Initialize your data structure here. */
    public Twitter() {
        messages=new HashMap<>();
        followers=new HashMap<>();
    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        Message m=new Message(userId,tweetId,order++);
        Set<Message> set=messages.getOrDefault(userId,new HashSet<>());
        set.add(m);
        messages.put(userId,set);
    }

    /** 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<Message>sets=new ArrayList<>();
        Set<Message> set=messages.getOrDefault(userId,new HashSet<>());

        sets.addAll(set);
        Set<Integer> follow=followers.get(userId);
        if (follow!=null){
            for (int i:follow){
                set=messages.getOrDefault(i,new HashSet<>());
                sets.addAll(set);
            }
        }
        List<Integer> res=new ArrayList<>();
        sets.sort(new Comparator<Message>() {
            @Override
            public int compare(Message o1, Message o2) {
                return o2.order-o1.order;
            }
        });
        for (int i=0;i<sets.size() && i<10;i++){
            Message m=sets.get(i);
            res.add(m.twitterId);
        }
        return res;
    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if (followeeId==followerId) return;
        Set<Integer> set=followers.getOrDefault(followerId,new HashSet<>());
        set.add(followeeId);
        followers.put(followerId,set);
    }

    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        if (followeeId==followerId) return;
        if (followers.containsKey(followerId)){
            Set<Integer> set=followers.get(followerId);
            set.remove(followeeId);
            if (set.size()==0){
                followers.remove(followerId);
            }else{
                followers.put(followerId,set);
            }
        }
    }
}

  

原文地址:https://www.cnblogs.com/xiaobaituyun/p/11027978.html