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

  • 相关阅读:
    windows下的mysql备份恢复命令
    sp_addlinkedserver的使用方法 (转)
    一个以pubs数据库为例的SQL SERVER数据库全文索引
    【C#】可空类型(Nullable)
    【C#】 异常处理
    【电脑常识】如何查看电脑是32位(X86)还是64位(X64),如何知道硬件是否支持64位系统
    【C#】委托Delegate
    【工具】VS2010常用调试技巧(1)
    实验1 总结
    用C语言编程自动生成四则运算
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12689711.html
Copyright © 2011-2022 走看看