题目连接

https://leetcode.com/problems/design-twitter

Design Twitte

Description

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods: 
1. postTweet(userId, tweetId): Compose a new tweet. 
2. getNewsFeed(userId): 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. 
3. follow(followerId, followeeId): Follower follows a followee. 
4. unfollow(followerId, followeeId): Follower unfollows a followee.

Example:

Twitter twitter = new Twitter();// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5);// User 1's news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1);// User 1 follows user 2.
twitter.follow(1, 2);// User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6);// User 1's news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1);// User 1 unfollows user 2.
twitter.unfollow(1, 2);// User 1's news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);

根据题意直接模拟即可, 
我用了一个固定大小的set来保存10个最近的id 
如果tweets少于10个直接插入即可,否则每插入一个就与set的最后一个元素判断 
(已重载比较函数,set里面的元素是以时间戳从大到小排序的)

PS:好久都没有更新blogs了,太懒了(⊙﹏⊙)b

class Twitter {
private:struct P {int id, ref;P(int i = 0, int j = 0) :id(i), ref(j) {}inline bool operator<(const P &a) const { return ref > a.ref; }};
public:Twitter() { time = 1; }void postTweet(int userId, int tweetId) {userPost[userId].insert(P(tweetId, time++));userFollow[userId].insert(userId);}vector<int> getNewsFeed(int userId) {q.clear();vector<int> res;for(auto &r: userFollow[userId]) {int n = userPost[r].size();auto it = userPost[r].begin();n = min(n, 10);while(n--) {if(q.size() < 10) {q.insert(*it++);} else {auto c = q.end();if(*it < *--c) {q.erase(c);q.insert(*it++);}}}}for(auto &r: q) res.push_back(r.id);return res;}void follow(int followerId, int followeeId) {userFollow[followerId].insert(followeeId);}void unfollow(int followerId, int followeeId) {if(followerId == followeeId) return;userFollow[followerId].erase(followeeId);}
private:int time;set<P> q; unordered_map<int, set<P>> userPost;unordered_map<int, set<int>> userFollow;
};

转载于:https://www.cnblogs.com/GadyPu/p/5578594.html

leetcode 355 Design Twitte相关推荐

  1. leetcode 355. Design Twitter | 355. 设计推特(Java)

    题目 https://leetcode.com/problems/design-twitter/ 题解 这题不难,就是业务代码..写就是了.不知道为啥是 medium 题. class Twitter ...

  2. leetcode 622. Design Circular Queue | 641. 设计循环双端队列(Java)

    题目 https://leetcode.com/problems/design-circular-deque/ 题解 相关问题:leetcode 622. Design Circular Queue ...

  3. Leetcode 355. 设计推特 C++

    Leetcode 355. 设计推特 题目 设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个 ...

  4. LeetCode 705. Design HashSet (设计哈希集合)

    题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java ...

  5. leetcode 622. Design Circular Queue | 622. 设计循环队列(Ring Buffer)

    题目 https://leetcode.com/problems/design-circular-queue/ 题解 Ring Buffer 的实现,rear 指向新插入的位置,front 指向最旧的 ...

  6. leetcode 211. Design Add and Search Words Data Structure | 211. 添加与搜索单词 - 数据结构设计(Java)

    题目 https://leetcode.com/problems/design-add-and-search-words-data-structure/ 题解 字典树 + 深度优先搜素,思路参考 le ...

  7. LeetCode 705 Design HashSet 解题报告

    题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...

  8. LeetCode 1500. Design a File Sharing System(哈希map+优先队列)

    文章目录 1. 题目 2. 解题 1. 题目 We will use a file-sharing system to share a very large file which consists o ...

  9. LeetCode 355. 设计推特(哈希map+set)

    1. 题目 设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能: postTweet(us ...

最新文章

  1. 馒头,国庆节快乐啊!!~~~!
  2. 牛人写SCI常用经典词和常用句型
  3. ajax 分页 评论刷新,评论:js无刷新分页(原创)
  4. linux中的权限对于文件和目录的重要性
  5. 现实世界的Windows Azure:就Metanga采访MetraTech公司CEO,Scott Swartz先生
  6. 基于离线策略的强化学习(PPO)
  7. [python]LDA模型使用流程及代码
  8. 【FPGA】:ip核----ram based shift register
  9. ios闹钟铃声实现代码
  10. 谈谈奋斗里陆涛为什么不爱米莱
  11. JAVA创建一个Box类(长方体),在Box类中定义三个变量,分别表示长方体的长(length)、宽(width)和高(heigth)
  12. 光模块字母含义及参数简称大全
  13. Docker 高级篇
  14. 大家不用买无线路由器了也可以让手机上WIFI
  15. 我告诉你一个 AtomicInteger 的惊天大秘密
  16. JAMA Neurology:帕金森病跨疾病阶段的新兴神经成像生物标记物
  17. 攻克论文写作系列之5:你论文的“贡献”是什么?
  18. 安卓开发--连接到聚合网,获取JSON数据并解析(踩了好多好多坑)
  19. 如何使用Python创建AI虚拟助手
  20. html5录像特效,6个迷人而令人惊叹的HTML5动画特效

热门文章

  1. 神舟服务器安装系统,神舟UT47笔记本一键u盘装系统win10教程
  2. BIM正向设计是什么
  3. 2019 面试准备 - 图片
  4. 利用系统方法分析COBIT5解决问题的原理
  5. 金蝶财务软件服务器文件是什么,金蝶财务软件 远程服务器
  6. 佳能ix4000ix5000打印机清零
  7. 0040.大家来找茬(二).VIP课程
  8. 如何用Gitbook制作电子书?
  9. 看完嵌入式培训视频的一些感受
  10. 2005考研阅读Text1翻译