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

  • 相关阅读:
    js 为表格增加行 动态
    百度测试新搜索结果页面 改进灵感来自谷歌?
    多线程程序中使用fork的问题
    C++ struct和class的区别
    J2SE 5 HotSpot JVM 解释
    并行优化、xvout
    C++基础:纯虚函数和抽象类
    C++的四种cast
    logcat过滤输出
    C++虚函数和纯虚函数(2)
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12689711.html
Copyright © 2011-2022 走看看