"Leetcode" 355. Design Twitter

Source: Internet
Author: User
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&gt
        ;();
        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&gt
        ;();
        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); */


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.