Skip to content

Commit

Permalink
Update Readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Sep 26, 2019
1 parent 350a549 commit 37efc58
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Design/355.Design-Twitter/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ int global_id; // 全局变量,记录所有推文的时间顺序。
unordered_map<int,vector<pair<int,int>>>Tweets; // 记录每个用户发推的global_id和tweetID。

在getNewsFeed时,遍历所有好友的推文记录,寻找最近的10个。
基本思路是构造一个数据结构news,每次放进一个推文,news能够自动保持根据global_id保持有序。什么数据结构能实现这个功能呢?有两个方案:priority_queue,或者有序map(set亦可)。考虑到map里的元素已经是pair,而map的自动排序就是依照pair的第一个key从小到大,在这里更为方便。
基本思路是构造一个数据结构news,每次放进一个推文,news能够自动保持根据global_id保持有序。什么数据结构能实现这个功能呢?有两个方案:priority_queue,或者有序set。考虑到set里的元素已经是pair,并且其自动排序就是依照pair的第一个key从小到大,在这里更为方便。
```cpp
      map<int,int>news; //第一个key是推文的global_id,第二个才是其tweetId
      set<pair<int,int>>news; //第一个key是推文的global_id,第二个才是其tweetId
      for (auto i:friends[userId]) //遍历所有userId的好友
      {
          for (int j=tweets[i].size()-1; j>=0; j--) //遍历该好友的所有推文,从最近的开始
Expand Down

0 comments on commit 37efc58

Please sign in to comment.