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:java

 

  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:ide

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條消息等函數。post

解法:this

其實仍是主要要實現兩個map,一個map存放着用戶id到用戶關注列表的映射,一個map存放着用戶id到用戶所發消息的映射。order是記錄消息所發的時間順序。blog

java:rem

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);
            }
        }
    }
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息