355. Design Twitter My Submissions Questioneditorial Solution
Total accepted:731 Total submissions:3056 difficulty:medium
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 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 = 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.< C15/>twitter.getnewsfeed (1);
"Problem-solving ideas"
HashMap 1, pay attention to get the latest articles, repeat data processing.
2, each time to add new articles, you can put in the list, in order to save, to avoid sorting.
Java AC
public class Twitter {private Map<integer, list<integer>> Followmap;
Private Map<integer, list<integer>> Tweetmap;
Private list<integer> tweetlist;
/** * Initialize your data structure here.
*/Public Twitter () {followmap = new Hashmap<integer, list<integer>> ();
Tweetmap = new Hashmap<integer, list<integer>> ();
Tweetlist = new arraylist<integer> ();
}/** * Compose a new tweet.
*/public void Posttweet (int userId, int tweetid) {list<integer> List = new arraylist<integer> ();
if (Tweetmap.containskey (userid)) {list = Tweetmap.get (userid);
} list.add (Tweetid);
Tweetmap.put (userId, list);
Tweetlist.add (Tweetid);
}/** * 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. */Public list<integer> getnewsfeed (int userId) {list<integer> List = new Arraylist<integer>
;();
if (Tweetmap.containskey (userid)) {list = Tweetmap.get (userid);
} list<integer> alllist = new arraylist<integer> (List);
list<integer> followees = new arraylist<integer> ();
if (Followmap.containskey (userid)) {followees = Followmap.get (userid);
} for (int i = 0; i < followees.size (); i++) {if (Tweetmap.containskey (Followees.get (i))) {
Alllist.addall (Tweetmap.get (Followees.get (i)));
}} set<integer> Set = new hashset<integer> (alllist);
list<integer> feedlist = new arraylist<integer> ();
int k = 0; for (int i = Tweetlist.size ()-1; I >= 0; i--) {if (k == ten) {break;
} int tweetid = Tweetlist.get (i);
if (Set.contains (Tweetid)) {Feedlist.add (Tweetid);
k++;
}} return feedlist; }/** * Follower follows a followee.
If The operation is invalid, it should be a no-op. */public void follow (int followerid, int followeeid) {list<integer> List = new Arraylist<integer>
;();
if (Followmap.containskey (Followerid)) {list = Followmap.get (Followerid);
} if (!list.contains (Followeeid)) {List.add (Followeeid);
} followmap.put (Followerid, list); }/** * Follower unfollows a followee.
If The operation is invalid, it should be a no-op.
*/public void unfollow (int followerid, int followeeid) {if (Followmap.containskey (Followerid)) {
list<integer> list = Followmap.get (Followerid); List.remove ((Integer) Followeeid);
Followmap.put (Followerid, list);
}}}/** * Your Twitter object would be instantiated and called as such: * twitter obj = new Twitter ();
* Obj.posttweet (Userid,tweetid);
* List<integer> param_2 = obj.getnewsfeed (userId);
* Obj.follow (Followerid,followeeid);
* Obj.unfollow (Followerid,followeeid); */