355. Design Twitter
Test instructions: Design the Twitter API to achieve the following features.
- Posttweet (UserId, Tweetid): Compose a new tweet.
- Getnewsfeed (userId): Retrieve The most recent tweets IDs in the user ' s news Feed. Each item in the news feed must is posted by the users of the user followed or by the user herself. Tweets must is ordered from the most recent to least recent.
- Follow (Followerid, Followeeid): Follower follows a followee.
- Unfollow (Followerid, Followeeid): Follower unfollows a followee.
Example:
twitter Twitter =NewTwitter ();//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);
Here is my implementation code
General idea: The global information queue, sorted by publication time. Global User Mapping table, save user information, design a hash table structure, easy to get users quickly.
1 Public classTwitter {2 3 classUserImplementsComparable<user>{4 intuserId;5List<user>followers;6List<user>followees;7User (intuserId) {8 This. UserId =userId;9 This. followers =NewLinkedlist<>();Ten This. Followees =NewLinkedlist<>(); One } A Public intcompareTo (User o) { - returnuserid-O.userid; - } the } - - classmessage{ - intuserId; + intmessageId; -Message (intUseridintmessageId) { + This. UserId =userId; A This. MessageId =messageId; at } - } - - PrivateList<message>messages; - PrivateMap<integer, user>users; - in /**Initialize your data structure here.*/ - PublicTwitter () { toMessages =NewLinkedlist<>(); +Users =NewHashmap<>(); - } the * /**Compose a new tweet.*/ $ Public voidPosttweet (intUseridintTweetid) {Panax Notoginseng - if(!Users.containskey (userId)) { theUser U =NewUser (userId); + Users.put (userid,u); A } the +Message m =NewMessage (UserId, tweetid); -Messages.add (0, m); $ } $ - /**Retrieve The most recent tweets IDs in the user's news feed. Each item in the news feed must is posted by the users of the user followed or by the user herself. Tweets must is ordered from the most recent to least recent. */ - PublicList<integer> Getnewsfeed (intuserId) { theList<integer> newmessages =NewArraylist<>(); -set<integer> all =NewHashset<>();Wuyi All.add (userId); the if(Users.containskey (userId)) { - for(User u:users.get (userId). followees) { Wu All.add (U.userid); - } About } $ intI=0; - for(Message m:messages) { - if(All.contains (M.userid)) { - Newmessages.add (M.messageid); Ai++; + if(i==10) Break; the } - } $ the returnnewmessages; the } the the /**Follower follows a followee. If The operation is invalid, it should be a no-op.*/ - Public voidFollow (intFollowerid,intFolloweeid) { in User U1; the User U2; the if(Users.containskey (Followerid)) { Aboutu1 =Users.get (Followerid); the}Else{ theu1 =NewUser (Followerid); the } + - if(Users.containskey (Followeeid)) { theU2 =Users.get (Followeeid);Bayi}Else{ theU2 =NewUser (Followeeid); the } - U1.followees.add (U2); - U2.followers.add (U1); the Users.put (Followerid, U1); the Users.put (Followeeid, U2); the } the - /**Follower unfollows a followee. If The operation is invalid, it should be a no-op.*/ the Public voidUnfollow (intFollowerid,intFolloweeid) { theUser u1 =Users.get (Followerid); theUser U2 =Users.get (Followeeid);94 if(u1!=NULL) the U1.followees.remove (U2); the if(u2!=NULL) the U2.followers.remove (U1);98 } About}
Designing the Twitter API