[LeetCode] 355. Design Twitter Java

題目:ide

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:post

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. 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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. unfollow(followerId, followeeId): Follower unfollows a followee.

Example:this

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);
題意及分析:實現一個推特的結構,要求可以發推特,關注其餘用戶,而後獲取當前用戶或者其關注用戶的最新最多10條新推特。這裏我直接使用了一個ArrayList來保存全部用戶發表的推特。
而後一個hashmap來保存當前用戶關注的其餘用戶。須要注意的是在獲取推特的時候,須要查看本用戶發佈的推特。
代碼:
class News{
    int user;
    int tweet;
    public News(int userId,int tweetId){
        user = userId;
        tweet = tweetId;
    }
}

public class Twitter {

   HashMap<Integer,ArrayList<Integer>> followList; //第一個參數爲userId,後一個參數爲其關注的人的列表
    ArrayList<News> news;
    /** Initialize your data structure here. */
    public Twitter() {
        followList = new HashMap<>();
        news = new ArrayList<>();
    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if(userId==1&&tweetId==6417) System.out.println("here6417");
        if(userId==8&&tweetId==3555) System.out.println("here");
        news.add(new News(userId,tweetId));
    }

    /** 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> res = new ArrayList<>();
        ArrayList<Integer> peoples = followList.get((Object)userId);        //找到這我的關注的全部人
        int count=0;
        int length = news.size()-1;
        while(count<10&&length>=0){
            News temp = news.get(length);
            if((peoples!=null&&peoples.contains(temp.user))||userId==temp.user){    //是本身或者關注的推特
                if(!res.contains(temp.tweet)){      //不包含重複的
                    res.add(temp.tweet);
                    count++;
                }
            }
            length--;
        }
        return res;
    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if(followList.get(followerId)==null){       //沒有follower的關注人列表
            ArrayList<Integer> arrayList = new ArrayList<>();
            arrayList.add(followeeId);
            followList.put(followerId,arrayList);
            return;
        }

        if(!followList.get(followerId).contains(followeeId)){       //Follower has not followed followee
            followList.get(followerId).add(followeeId);
        }
    }

    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        if(followList.get(followerId)==null)        //若是沒有該用戶的關注列表,直接返回
            return;
        if(followList.get(followerId).contains(followeeId)){       //若是Follower關注了followee
            followList.get(followerId).remove((Object)followeeId);
            if(followList.get(followerId).size()==0)        //若是把全部的都刪除了
                followList.remove(followerId);
        }
    }
}

/**
 * Your Twitter object will 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);
 */
View Code
 
 

 

 
 

 

 


 
Seen this question in a real interview befo
相關文章
相關標籤/搜索