Is there something wrong with the testcase?


  • 0
    X

    class Twitter {
    private:
    unordered_map<int, vector<int>> user_map;
    unordered_map<int, vector<int>> user_tweet;
    public:
    /** Initialize your data structure here. */
    Twitter() {

    }
    
    /** Compose a new tweet. */
    void postTweet(int userId, int tweetId) {
        user_tweet[userId].push_back(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. */
    vector<int> getNewsFeed(int userId) {
        vector<int> res;
        int count = 0;
        for (int i = user_map[userId].size() - 1; i >= 0; --i) {
            if (user_map[userId].at(i) == INT_MIN) continue;
            for (int j = user_tweet[user_map[userId][i]].size() - 1; j >=0 ; --j) {
                if (count < 10) {
                    res.push_back(user_tweet[user_map[userId][i]][j]);
                    count++;
                }
            }
        }
        for (int k = user_tweet[userId].size() - 1; k >= 0 && count < 10; --k) {
            res.push_back(user_tweet[userId][k]);
            count ++;
        }
        return res;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    void follow(int followerId, int followeeId) {
        if (followerId == followeeId) return;
        for (auto &a : user_map[followerId]) {
            if (a == followeeId) return;
        }
        user_map[followerId].push_back(followeeId);
    }
    
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    void unfollow(int followerId, int followeeId) {
        for (int i = 0; i < user_map[followerId].size(); ++i) {
            if (user_map[followerId][i] == followeeId) user_map[followerId][i] = INT_MIN;
        }
    }
    

    };

    /**

    • Your Twitter object will be instantiated and called as such:
    • Twitter obj = new Twitter();
    • obj.postTweet(userId,tweetId);
    • vector<int> param_2 = obj.getNewsFeed(userId);
    • obj.follow(followerId,followeeId);
    • obj.unfollow(followerId,followeeId);
      */

    Here is my problem! 0_1496994229183_屏幕快照 2017-06-09 下午3.40.12.png
    while I think my output is the right one


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.