zoukankan      html  css  js  c++  java
  • 设计推特

    static int nowTweetTime = 0;
    struct TweetContext {
        int t_time;
        int t_id;
    };
    
    class UserInformation {
    public:
        std::vector<TweetContext> tList; // 推文列表
        std::set<int> followList;  // 关注列表
    
        void follow(int Id) {
            followList.insert(Id);
        }
        void unfollow(int Id) {
            followList.erase(Id);
        }
        void post(int Id) {
            TweetContext tweeter;
            tweeter.t_id = Id;
            tweeter.t_time = ++nowTweetTime;
            tList.emplace_back(tweeter);
            if (tList.size() > 10) {
                tList.erase(tList.begin());
            }
        }
    };
    
    class Twitter {
    public:
        /** Initialize your data structure here. */
        Twitter() {
            
        }
        
        /** Compose a new tweet. */
        void postTweet(int userId, int tweetId) {
            userMap[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) {
            UserInformation user = userMap[userId];
            std::vector<TweetContext> newTweeterList;
            std::vector<int> feed;
    
            std::copy(user.tList.begin(), user.tList.end(), std::back_inserter(newTweeterList));  // 用户推文拷贝到新的推文中
            for(auto id : user.followList) { // 将关注列表的推文也放入新的推文中
                std::copy(userMap[id].tList.begin(), userMap[id].tList.end(), std::back_inserter(newTweeterList));
            }
    
            // 选出最近十条推文
            std::sort(newTweeterList.begin(), newTweeterList.end(), [](TweetContext& a, TweetContext& b){
                    return a.t_time > b.t_time; 
            });
            int size = newTweeterList.size();
            if (size < 10) {
                for (int i = 0; i < size; ++i) {
                    feed.emplace_back(newTweeterList[i].t_id);
                }
            }
            else {
                for (int i = 0; i < 10; ++i) {
                    feed.emplace_back(newTweeterList[i].t_id);
                }
            }
            return feed;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        void follow(int followerId, int followeeId) {
            if (followerId != followeeId)
                userMap[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 (userMap.find(followerId) == userMap.end()) return;
            userMap[followerId].unfollow(followeeId);
        }
    private:
        std::map<int, UserInformation> userMap;
    };
    
    /**
     * 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);
     */
    

    reference

    leetcode

  • 相关阅读:
    Linux centosVMware iptables规则备份和恢复、firewalld的9个zone、firewalld关于zone的操作、firewalld关于service的操作
    Linux centos7 Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法
    Linux centos7日常运维——监控io性能、free内存命令、ps进程命令、查看网络状态、linux下抓包
    三、haproxy反向代理
    二、NGINX反向代理
    一、APACHE反向代理
    一、Zabbix安装
    KVM安装测试
    三、CentOS7.4下kibana6.2.4安装
    二、CentOS7.4下Logstash6.2.4安装
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12689711.html
Copyright © 2011-2022 走看看