LintCode 迷你推特

实现一个迷你的推特,支持下列几种方法
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.


解法:

有趣的题目,用vector保存Tweet,map保存follow关系,逻辑写清楚就行了。

class MiniTwitter {
public:
    vector<Tweet> tweets;
    map<int, vector<int> > follows;

    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
        tweets.push_back(Tweet::create(user_id, tweet_text));
        return tweets[tweets.size() - 1];
    }

    // @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<int>* flist = NULL;
        if(follows.count(user_id)) {
            flist = &follows[user_id];
        }
        if(flist == NULL || flist->size() == 0) {
            return getTimeline(user_id);
        }
        vector<Tweet> r;
        for(int i = tweets.size() - 1; i >=0; --i) {
            if(tweets[i].user_id == user_id) {
                r.push_back(tweets[i]);
            }else if(find(flist->begin(), flist->end(), tweets[i].user_id) 
                    != flist->end()) {
                r.push_back(tweets[i]);            
            }
            if(r.size() == 10) return r;
        }
        return r;
    }
        
    // @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> r;
        for(int i = tweets.size() - 1; i >=0; --i) {
            if(tweets[i].user_id == user_id) {
                r.push_back(tweets[i]);
                if(r.size() == 10) return r;
            }
        }
        return r;
    }

    // @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
        if(follows.count(from_user_id)) {
            vector<int>& arr = follows[from_user_id];
            if(find(arr.begin(), arr.end(), to_user_id) == arr.end()) {
                arr.push_back(to_user_id);    
            }
        }else {
            vector<int> arr(1, to_user_id);
            follows[from_user_id] = arr;    
        }
    }

    // @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
        if(follows.count(from_user_id)) {
            vector<int>& arr = follows[from_user_id];
            auto it = find(arr.begin(), arr.end(), to_user_id);
            if(it != arr.end()) {
                arr.erase(it);
            }
        }
    }
};


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值