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

  • 相关阅读:
    Handler详细说明系列(六)——View的post()详解
    2013年6月17日星期一
    2013年6月16日星期日
    线程管理四种方法
    java 线程 ProducerAndConsumer
    POJ 2948 DP
    Java的递归算法
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    网络安全审查制度即将推出 手机App安全加密成必定趋势
    递归算法浅谈
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12689711.html
Copyright © 2011-2022 走看看