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表结构,便于快速获取用户。
1 public class Twitter { 2 3 class User implements Comparable<User>{ 4 int userId; 5 List<User> followers; 6 List<User> followees; 7 User(int userId) { 8 this.userId = userId; 9 this.followers = new LinkedList<>(); 10 this.followees = new LinkedList<>(); 11 } 12 public int compareTo(User o){ 13 return userId-o.userId; 14 } 15 } 16 17 class Message{ 18 int userId; 19 int messageId; 20 Message(int userId, int messageId){ 21 this.userId = userId; 22 this.messageId = messageId; 23 } 24 } 25 26 private List<Message> messages; 27 private Map<Integer, User> users; 28 29 /** Initialize your data structure here. */ 30 public Twitter() { 31 messages = new LinkedList<>(); 32 users = new HashMap<>(); 33 } 34 35 /** Compose a new tweet. */ 36 public void postTweet(int userId, int tweetId) { 37 38 if(!users.containsKey(userId)){ 39 User u = new User(userId); 40 users.put(userId,u); 41 } 42 43 Message m = new Message(userId, tweetId); 44 messages.add(0, m); 45 } 46 47 /** 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. */ 48 public List<Integer> getNewsFeed(int userId) { 49 List<Integer> newMessages = new ArrayList<>(); 50 Set<Integer> all = new HashSet<>(); 51 all.add(userId); 52 if(users.containsKey(userId)) { 53 for (User u : users.get(userId).followees) { 54 all.add(u.userId); 55 } 56 } 57 int i=0; 58 for(Message m : messages){ 59 if(all.contains(m.userId)){ 60 newMessages.add(m.messageId); 61 i++; 62 if(i==10) break; 63 } 64 } 65 66 return newMessages; 67 } 68 69 /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ 70 public void follow(int followerId, int followeeId) { 71 User u1; 72 User u2; 73 if(users.containsKey(followerId)){ 74 u1 = users.get(followerId); 75 }else{ 76 u1 = new User(followerId); 77 } 78 79 if(users.containsKey(followeeId)){ 80 u2 = users.get(followeeId); 81 }else{ 82 u2 = new User(followeeId); 83 } 84 u1.followees.add(u2); 85 u2.followers.add(u1); 86 users.put(followerId, u1); 87 users.put(followeeId, u2); 88 } 89 90 /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ 91 public void unfollow(int followerId, int followeeId) { 92 User u1 = users.get(followerId); 93 User u2 = users.get(followeeId); 94 if(u1!=null) 95 u1.followees.remove(u2); 96 if(u2!=null) 97 u2.followers.remove(u1); 98 } 99 }
时间: 2024-10-12 04:58:45