design-twitter
https://leetcode.com/problems/design-twitter/ class Twitter {
unordered_map<int, set<int> > follower_mp;
unordered_map<int, set<int> > followee_mp;
unordered_map<int, vector<pair<int, int> > > twitter_self_mp;
int timestamp;
public:
/** Initialize your data structure here. */
Twitter() {
timestamp = ;
} /** Compose a new tweet. */
void postTweet(int userId, int tweetId) { if (twitter_self_mp.find(userId) == twitter_self_mp.end()) {
vector<pair<int, int> > tmp_vec;
twitter_self_mp[userId] = tmp_vec;
}
timestamp++;
twitter_self_mp[userId].insert(twitter_self_mp[userId].begin(), make_pair(timestamp, 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) {
set<int> tmp_set;
if (follower_mp.find(userId) != follower_mp.end()) {
tmp_set = follower_mp[userId];
}
tmp_set.insert(userId); vector<pair<int, int> > result; set<int>::iterator itr = tmp_set.begin();
for (; itr != tmp_set.end(); ++itr) {
if (twitter_self_mp.find(*itr) == twitter_self_mp.end()) {
continue;
} int curPos = ;
int curSize = result.size(); vector<pair<int, int> > tmp_vec = twitter_self_mp[*itr];
vector<pair<int, int> >::iterator vitr = tmp_vec.begin(); for (; vitr != tmp_vec.end() && curPos < ; ++vitr) {
while (curPos < && curPos < curSize &&
result[curPos].first > vitr->first) { curPos++;
} if (curPos >= ) {
break;
} result.insert(result.begin() + curPos,
*vitr);
curPos++;
curSize++;
}
} vector<int> ret;
int rlen = result.size();
for (int i=; i< && i<rlen; ++i) {
ret.push_back(result[i].second);
}
return ret; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
if (follower_mp.find(followerId) == follower_mp.end()) {
set<int> tmp_set;
follower_mp[followerId] = tmp_set;
}
follower_mp[followerId].insert(followeeId); if (followee_mp.find(followeeId) == followee_mp.end()) {
set<int> tmp_set;
followee_mp[followeeId] = tmp_set;
}
followee_mp[followeeId].insert(followerId); } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
if (follower_mp.find(followerId) != follower_mp.end() &&
follower_mp[followerId].find(followeeId) != follower_mp[followerId].end()) {
follower_mp[followerId].erase(followeeId);
} if (followee_mp.find(followeeId) != followee_mp.end() &&
followee_mp[followeeId].find(followerId) != followee_mp[followeeId].end()) {
followee_mp[followeeId].erase(followerId);
} }
}; /**
* 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);
*/
design-twitter的更多相关文章
- [LeetCode] Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 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 us ...
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode "Design Twitter"
A mix of hashmap, list and heap. struct Tw { Tw(long long pts, int tid) { ts = pts; tweetid = tid; } ...
- 355. Design Twitter
二刷尝试了别的办法,用MAP代表关注列表. 然后不初始化,但是只要有用户被使用,而他又不在MAP里,就把他加进去,然后让他关注自己.. 但是这样做超时了. 问题在于这个题解法太多,有很多不同的情况. ...
- 355 Design Twitter 设计推特
设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能: postTweet(userI ...
- [leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
随机推荐
- curl之采集QQ空间留言
目录 主要流程解析 注意事项 扩展 完整代码示例 采集效果一览 主要流程解析 首先,打开浏览器登录QQ空间并访问留言列表 由于QQ空间的链接是https,curl方式请求https链接需要突破http ...
- [HDU - 5408] CRB and Farm
题意: 给出一个由n个点组成的凸包,以及凸包内的k个点,问是否能够在凸包上选择最多2k个点构造一个新的 凸包,使得新的凸包覆盖原来的k个点. 要求2k个点覆盖原本的k个点,只要对原k个点构造凸包,然后 ...
- React Native网络编程之Fetch
目录 1.前言 2.什么是Fetch 3.最简单的应用 4.支持的请求参数 - 4.1. 参数详讲 - 4.2. 示例 5.请求错误与异常处理 1. 前言 网络请求是开发APP中不可或缺的一部 ...
- Tensorflow学习:(三)神经网络优化
一.完善常用概念和细节 1.神经元模型: 之前的神经元结构都采用线上的权重w直接乘以输入数据x,用数学表达式即,但这样的结构不够完善. 完善的结构需要加上偏置,并加上激励函数.用数学公式表示为:.其中 ...
- read file into shell vars
test.ksh value=$(<rosstest.txt)echo $value
- Linux驱动程序中的并发控制
<临界区> a:对共享资源进行访问的代码称为临界区. <原子操作> a:原子操作用于执行轻量级,仅仅执行一次的的操作比如修改计数器,有条件的增加值,设置某一位.所谓 ...
- Python 面向对象编程——继承和多态
<基本定义> 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超 ...
- django组件之contenttype(一)
方式1:适用于1张表和另一张表要关联的时候. 1.路飞学成表设计: 2.将2个价格策略表合并1张表. 3.如果再加一张表,那价格策略表的表结构会发生改变. 这样不合理的,我们的表结构一般设计完就不会 ...
- LeetCode:整数反转(Reserve Integer)
public class ReserveInteger { public int reverse(int x) { //用于接收个位数(10的余数) int remainder; //是否负数 int ...
- JBOSS集群和安装
JBOSS集群和安装 http://jijian91.com/blog20071010/jboss-cluster-part5.html http://wing123.iteye.com/blog/3 ...