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:
- 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);
题目大意:
实现可以关注关注人,取消关注人,获取最新的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); } } } }