Leetcode 335.设计推特

设计推特

一个设计题目,在题解区看见了一个比较好的面向对象版本,写起来真跟写工程一样
coding 10分钟,debug 俩小时…
其实这大概便是真实场景的简化版了吧,写着写着我想到了瀑布流,想到了推荐系统…
参考:面向对象

int global_time = 0;
int maxRecent = 10;
class Tweets{
public:
    int tweetId;
    int time;
    Tweets(int id){
        tweetId = id;
        global_time ++;
        time =  global_time;
    }
    bool operator<(const Tweets& b){
        return time<b.time;
    }
};
class User{
public:
    int userId;
    list<Tweets*> tweet_list;
    unordered_set<int> followee_set;
    User(int id){
        userId = id;
        // followee_set.clear();
        // tweet_list.clear();
    }
    void post(int tweetId){
        Tweets *newTweet = new Tweets(tweetId);
        // 达到限制,剔除链表末尾元素,可以节省空间和时间
        if (tweet_list.size() == maxRecent) {
            tweet_list.pop_back();
        }
        tweet_list.push_front(newTweet);
    }
    void follow(int id){
        if(userId==id) return ;
        followee_set.insert(id);
    }
    void unfollow(int id){
        if(followee_set.find(id)==followee_set.end()||userId==id) return ;
        followee_set.erase(id);
    } 
};

class Twitter {
    /** Initialize your data structure here. */
    unordered_map<int,User*> user_map;
    
public:
    Twitter() {
        user_map.clear();
    }
    bool isContain(int userId){
        return user_map.find(userId)!=user_map.end();
    }
    /** Compose a new tweet. */
    void postTweet(int userId, int tweetId) {
        if(!isContain(userId)) user_map[userId] = new User(userId);
        user_map[userId]->post(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; res.clear();
        if(user_map.find(userId)==user_map.end()) return res;
        priority_queue<Tweets*,vector<Tweets*>> qu;
        list<Tweets*>::iterator it = user_map[userId]->tweet_list.begin();
        while(it!=user_map[userId]->tweet_list.end()) {qu.push(*it);it++;}
        for(int id:user_map[userId]->followee_set){
            if(!isContain(id)||id==userId) continue;
            it = user_map[id]->tweet_list.begin();
            while(it!=user_map[id]->tweet_list.end()) {qu.push(*it);it++;}
        }
        while(qu.size()){
            Tweets *tweet = qu.top(); qu.pop();
            res.push_back(tweet->tweetId);
            if(res.size()==maxRecent) break;
        }
        return res;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    void follow(int followerId, int followeeId) {
        if(!isContain(followerId)) user_map[followerId] = new User(followerId);
        if(!isContain(followeeId)) user_map[followeeId] = new User(followeeId);
        user_map[followerId]->follow(followeeId);
    }
    
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    void unfollow(int followerId, int followeeId) {
        if(!isContain(followerId)) return ;
        // user_map[followerId] = new User(followerId);
        user_map[followerId]->unfollow(followeeId);
    }
};

/**
 * 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);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值