leetcode 355 Design Twitte
题目连接
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;
};
leetcode 355 Design Twitte的更多相关文章
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- [leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- #Leetcode# 707. Design Linked List
https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...
随机推荐
- leetcode:7. Reverse Integer
这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch public int ...
- 【PHP】composer 常用命令
- iOS模拟器的应用沙盒在MAC中的位置
每个iOS应用都有自己专属的应用沙盒.分别为 应用程序包 Documents/ Library/Caches/ Library/Preferences/ tmp/ 当运行模拟时,在MAC下找到对应路径 ...
- Spring MVC 基于URL的映射规则(注解版)
好几天没有跟进Spring MVC的学习了,之前看了点源码都忘的差不多了.这次就跟着之前的问题,继续总结下Spring MVC中的小知识. 关于SpringMVC的小demo可以参考这里! url-p ...
- thinkphp5文件上传问题
tp5中文件上传如果没有数据就会报错,所以要先做一个判断 //先接收文件数据 $isfile=$_FILES;//判断是否上传图片数据,如果没有上传数据二位数组中的name会为空,如下例:if($is ...
- 通过ps给透明通道的图片添加灰度(适用于需要兼容IE7,效果很好)
原始的图片是这样的 第一步: 第二步: 第三步: 第四步: 更多(文字居中): 1: 2: 3: 4:
- 分别使用http,express,koa请求第三方接口
今天又再次恶补了一下http的内容,确切地说是node.js里面的http的内容,啊,百度了半天express怎么请求第三方接口,结果发现自己买的入门书籍都有这个内容.舍近求远,我真是醉了.还有百度上 ...
- Python Unittest - 根据不同测试环境跳过用例详解
本文章会讲述以下几个内容: 1.Unittest 如何跳过用例 2.如何使用sys.argv 3.自动化测试项目中如何一套代码多套环境运行 一.Unittest跳过用例 @unittest.skip( ...
- JS——操作元素属性
属性的操作包括:读和写 方法: 1)”.“操作 2)”[ ]“操作 eg: var oDiv = document.getElementById('div1'); var attr = 'color' ...
- OSI七层和TCP/IP四层的关系、TCP与UDP、HTTP、Socket
HTTP(应用层协议):超文本传输协议,HTTP协议是建立在TCP协议之上的一种应用. HTTP协议详细解释 2Http详解 TCP(面向连接的传输层协议):transmission control ...