lintcode-501-迷你推特

501-迷你推特

实现一个迷你的推特,支持下列几种方法

  1. postTweet(user_id, tweet_text). 发布一条推特.
  2. getTimeline(user_id). 获得给定用户最新发布的十条推特,按照发布时间从最近的到之前排序
  3. getNewsFeed(user_id). 获得给定用户的朋友或者他自己发布的最新十条推特,从发布时间最近到之前排序
  4. follow(from_user_id, to_user_id). from_user_id 关注 to_user_id.
  5. unfollow(from_user_id, to_user_id). from_user_id 取消关注 to_user_id.

样例

postTweet(1, "LintCode is Good!!!")
>> 1
getNewsFeed(1)
>> [1]
getTimeline(1)
>> [1]
follow(2, 1)
getNewsFeed(2)
>> [1]
unfollow(2, 1)
getNewsFeed(2)
>> []

思路

  • 使用数组记录所有 twitter 的信息,数组值的插入顺序就是所有用户新建 Twitter 的顺序,所以在寻找最新的 twitter 时,只需要从后向前遍历数组即可
  • 在寻找用户自己的 twitter 时,从后向前遍历数组,若此条 twitter 的发布用户是自己,即可以将此 twitter 加入返回列表
  • 在寻找用户关注的用户(包括用户自己)的 twitter 时,使用 map 记录某用户的关注用户列表(map<int, set>),从后向前遍历数组,若此条 twitter 的发布用户在此用户的关注列表中,即可以将此 twitter 加入返回列表
  • 关注用户就是在此用户的关注列表中新建一个关注信息,取关就是取消关注信息

code

/**
 * Definition of Tweet:
 * class Tweet {
 * public:
 *     int id;
 *     int user_id;
 *     String text;
 *     static Tweet create(int user_id, string tweet_text) {
 *         // This will create a new tweet object,
 *         // and auto fill id
 *     }
 * }
 */
class MiniTwitter {
private:
    vector<Tweet> Tweets;
    map<int, set<int>> follows;

public:
    MiniTwitter() {
        // initialize your data structure here.
    }

    // @param user_id an integer
    // @param tweet a string
    // return a tweet
    Tweet postTweet(int user_id, string tweet_text) {
        // Write your code here
        // 将用户自身加入其关注列表
        follows[user_id].insert(user_id);
        // 新建 twitter
        Tweet t = Tweet::create(user_id, tweet_text);
        Tweets.push_back(t);
        return t;
    }

    // @param user_id an integer
    // return a list of 10 new feeds recently
    // and sort by timeline
    vector<Tweet> getNewsFeed(int user_id) {
        // Write your code here
        vector<Tweet> result;
        // 在总 twitter 表中查找用户自己发布的 Twitter,最多十条
        for (int i = Tweets.size() - 1, count = 0; i >= 0 && count < 10; i--) {
            if (follows[user_id].find(Tweets[i].user_id) != follows[user_id].end()) {
                result.push_back(Tweets[i]);
                count++;
            }
        }
        return result;
    }

    // @param user_id an integer
    // return a list of 10 new posts recently
    // and sort by timeline
    vector<Tweet>  getTimeline(int user_id) {
        // Write your code here
        vector<Tweet> result;
        // 在总 twitter 表中查找用户关注的用户发布的 Twitter,最多十条
        for (int i = Tweets.size() - 1, count = 0; i >= 0 && count < 10; i--) {
            if (Tweets[i].user_id == user_id) {
                result.push_back(Tweets[i]);
                count++;
            }
        }
        return result;
    }

    // @param from_user_id an integer
    // @param to_user_id an integer
    // from user_id follows to_user_id
    void follow(int from_user_id, int to_user_id) {
        // Write your code here
        // 新建关注关系
        follows[from_user_id].insert(to_user_id);
    }

    // @param from_user_id an integer
    // @param to_user_id an integer
    // from user_id unfollows to_user_id
    void unfollow(int from_user_id, int to_user_id) {
        // Write your code here
        // 取消关注关系
        follows[from_user_id].erase(to_user_id);
    }
};
原文地址:https://www.cnblogs.com/libaoquan/p/7417647.html