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下Tomcat服务器-maven项目部署
    数据库设计感悟
    数据库设计规范
    从零到一: 代码调试
    Java泛型与反射的综合应用
    Eclipse中,tomcat插件方式启动项目
    集合查询表--Map
    集合线性表--List之LinkedList(队列与栈)
    集合线性表--List之ArrayList
    Java中的日期操作
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12689711.html
Copyright © 2011-2022 走看看