zoukankan      html  css  js  c++  java
  • Leetcode-355 Design Twitter(设计推特)

      1 struct cmp
      2 {
      3     bool operator()(const pair<int,int> p1, const pair<int,int> p2)
      4     {
      5         return p1.first > p2.first; //first的小值优先
      6     }
      7 };
      8 
      9 class Twitter
     10 {
     11     private:
     12         map<int,pair<set<int>,vector<pair<int,int>>>> PersonList;
     13         int time;
     14     public:
     15         /** Initialize your data structure here. */
     16         Twitter()
     17         {
     18             time = 0;
     19         }
     20 
     21         /** Compose a new tweet. */
     22         void postTweet(int userId, int tweetId)
     23         {
     24             auto ptr_to_user = PersonList.find(userId);
     25             if(ptr_to_user==PersonList.end())
     26             {
     27                 set<int> FollowList;
     28                 vector<pair<int,int>> PostList;
     29                 PostList.push_back(make_pair(time,tweetId));
     30                 FollowList.insert(userId);
     31                 PersonList.insert(make_pair(userId,make_pair(FollowList,PostList)));
     32             }
     33             else
     34             {
     35                 ptr_to_user -> second.second.push_back(make_pair(time,tweetId));
     36             }
     37             time ++;
     38         }
     39 
     40         /** 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. */
     41         vector<int> getNewsFeed(int userId)
     42         {
     43             vector<int> result;
     44             auto ptr_to_user = PersonList.find(userId);
     45             if(ptr_to_user==PersonList.end())
     46                 return result;
     47             auto ptr_to_followlist = ptr_to_user->second.first.begin();
     48 
     49             priority_queue<pair<int,int>, vector<pair<int,int>>, cmp> Q;
     50             for(auto followeeid:ptr_to_user->second.first)
     51             {
     52                 auto ptr_to_followee = PersonList.find(followeeid);
     53                 for(auto p:ptr_to_followee->second.second)
     54                 {
     55                     if(Q.size()<10)
     56                     {
     57                         Q.push(make_pair(p.first,p.second));
     58                     }
     59                     else
     60                     {
     61                         if(p.first>Q.top().first)
     62                         {
     63                             Q.pop();
     64                             Q.push(make_pair(p.first,p.second));
     65                         }
     66                     }
     67                 }
     68             }
     69 
     70             while(!Q.empty())
     71             {
     72                 result.emplace_back(Q.top().second);
     73                 Q.pop();
     74             }
     75             reverse(result.begin(),result.end());
     76             return result;
     77         }
     78 
     79         /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
     80         void follow(int followerId, int followeeId)
     81         {
     82             auto ptr_to_follower = PersonList.find(followerId);
     83             auto ptr_to_followee = PersonList.find(followeeId);
     84             if(ptr_to_follower==PersonList.end() && ptr_to_followee!=PersonList.end())
     85             {
     86                 set<int> FollowList;
     87                 vector<pair<int,int>> PostList;
     88                 FollowList.insert(followeeId);
     89                 FollowList.insert(followerId);
     90                 PersonList.insert(make_pair(followerId,make_pair(FollowList,PostList)));
     91             }
     92             else if(ptr_to_follower!=PersonList.end()&&ptr_to_followee==PersonList.end())
     93             {
     94                 set<int> FollowList;
     95                 vector<pair<int,int>> PostList;
     96                 FollowList.insert(followeeId);
     97                 PersonList.insert(make_pair(followeeId,make_pair(FollowList,PostList)));
     98                 ptr_to_follower -> second.first.insert(followeeId);
     99             }
    100             else if(ptr_to_follower==PersonList.end()&&ptr_to_followee==PersonList.end())
    101             {
    102                 set<int> FollowList;
    103                 vector<pair<int,int>> PostList;
    104                 FollowList.insert(followeeId);
    105                 PersonList.insert(make_pair(followeeId,make_pair(FollowList,PostList)));
    106                 set<int> FollowList_;
    107                 vector<pair<int,int>> PostList_;
    108                 FollowList_.insert(followeeId);
    109                 FollowList_.insert(followerId);
    110                 PersonList.insert(make_pair(followerId,make_pair(FollowList_,PostList_)));
    111             }
    112             else
    113             {
    114                 ptr_to_follower -> second.first.insert(followeeId);
    115             }
    116         }
    117 
    118         /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    119         void unfollow(int followerId, int followeeId)
    120         {
    121             if(followerId==followeeId)
    122                 return ;
    123             auto ptr_to_follower = PersonList.find(followerId);
    124             if(ptr_to_follower==PersonList.end())
    125             {
    126                 set<int> FollowList;
    127                 vector<pair<int,int>> PostList;
    128                 FollowList.insert(followerId);
    129                 PersonList.insert(make_pair(followerId,make_pair(FollowList,PostList)));
    130                 return ;
    131             }
    132             ptr_to_follower -> second.first.erase(followeeId);
    133         }
    134 };

     其实可以把初始化创建人的ID的过程给抽出来做成一个函数,不过这只博主就是懒辣~

  • 相关阅读:
    Y+的一些讨论
    MATLAB中FFT的使用方法
    插入排序、冒泡排序、选择排序——转载
    输出控制中时间延迟的几种方法
    模拟通信调制方式与通信设备
    模拟通信主要特点
    模拟通信数字信号
    模拟通信
    传真存储变换设备与入网方式
    静止图像通信
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9482496.html
Copyright © 2011-2022 走看看