355. Design Twitter
题意:设计Twitter的API,实现以下功能。
- 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);
以下是我的实现代码
大致思路:全局的信息队列,按发布时间排序。全局的用户映射表,存用户信息,设计成hash表结构,便于快速获取用户。
public class Twitter { class User implements Comparable<User>{
int userId;
List<User> followers;
List<User> followees;
User(int userId) {
this.userId = userId;
this.followers = new LinkedList<>();
this.followees = new LinkedList<>();
}
public int compareTo(User o){
return userId-o.userId;
}
} class Message{
int userId;
int messageId;
Message(int userId, int messageId){
this.userId = userId;
this.messageId = messageId;
}
} private List<Message> messages;
private Map<Integer, User> users; /** Initialize your data structure here. */
public Twitter() {
messages = new LinkedList<>();
users = new HashMap<>();
} /** Compose a new tweet. */
public void postTweet(int userId, int tweetId) { if(!users.containsKey(userId)){
User u = new User(userId);
users.put(userId,u);
} Message m = new Message(userId, tweetId);
messages.add(0, m);
} /** 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<Integer> newMessages = new ArrayList<>();
Set<Integer> all = new HashSet<>();
all.add(userId);
if(users.containsKey(userId)) {
for (User u : users.get(userId).followees) {
all.add(u.userId);
}
}
int i=0;
for(Message m : messages){
if(all.contains(m.userId)){
newMessages.add(m.messageId);
i++;
if(i==10) break;
}
} return newMessages;
} /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
User u1;
User u2;
if(users.containsKey(followerId)){
u1 = users.get(followerId);
}else{
u1 = new User(followerId);
} if(users.containsKey(followeeId)){
u2 = users.get(followeeId);
}else{
u2 = new User(followeeId);
}
u1.followees.add(u2);
u2.followers.add(u1);
users.put(followerId, u1);
users.put(followeeId, u2);
} /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
User u1 = users.get(followerId);
User u2 = users.get(followeeId);
if(u1!=null)
u1.followees.remove(u2);
if(u2!=null)
u2.followers.remove(u1);
}
}