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的更多相关文章

  1. [LeetCode] Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  2. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

  3. 【Leetcode】355. Design Twitter

    题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...

  4. [LeetCode] 355. Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  5. 【LeetCode】355. Design Twitter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. LeetCode "Design Twitter"

    A mix of hashmap, list and heap. struct Tw { Tw(long long pts, int tid) { ts = pts; tweetid = tid; } ...

  7. 355. Design Twitter

    二刷尝试了别的办法,用MAP代表关注列表. 然后不初始化,但是只要有用户被使用,而他又不在MAP里,就把他加进去,然后让他关注自己.. 但是这样做超时了. 问题在于这个题解法太多,有很多不同的情况. ...

  8. 355 Design Twitter 设计推特

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

  9. [leetcode]355. Design Twitter设计实现一个微博系统

    //先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...

  10. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

随机推荐

  1. Win7/Win10下搭建Go语言开发环境

    1 下载适合window版本的Go安装包,下载地址http://code.google.com/p/go/downloads/list 2 下载适合window本本的LiteIDE,下载后解压即可使用 ...

  2. luogu NOIp热身赛(2018-11-07)题解

    为什么前面的人都跑得那么快啊? QAQ T1:区间方差 题目大意:询问区间方差,支持单点修改 首先把方差的式子展开,得到 $$d = \frac{a_1 + ... a_n}{n} - \frac{a ...

  3. 网络抓包及Http Https通信协议分析

    Wireshark基本介绍和学习TCP三次握手 之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括ht ...

  4. 认识javascript中的作用域和上下文

    javascript中的作用域(scope)和上下文(context)是这门语言的独到之处,这部分归功于他们带来的灵活性.每个函数有不同的变量上下文和作用域.这些概念是javascript中一些强大的 ...

  5. BZOJ 2330 SCOI2011糖果 差分约束

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2819  Solved: 820 题目连接 http://www ...

  6. Notepad++源代码阅读——窗口封装与继承

    引言 近期在看Notepad++的源代码,学习学习Win32 原生API的开发技巧. 本文以Notepad++ 1.0版本的源代码为例讲解如何封装windows窗口,实现面向对象开发,如何通过窗口的继 ...

  7. ActiveMQ Cluster (ActiveMQ 集群) 配置

    构建高可用的ActiveMQ系统在生产环境中是非常重要的,对于这个apache的消息中间件实现高可用非常简单,只要在Apache ActiveMQ单点基本配置基础上做一次配置变更(如果在一台设备上部署 ...

  8. Chrome DevTools Protocol Viewer

    Chrome DevTools Protocol Viewer     awesome-chrome-devtools

  9. IP配制

    http://blog.csdn.net/laoyiin/article/details/5722977

  10. STM32学习笔记3-IO配置输入输出

    STM32的IO配置时没什么特殊的,有个注意点就是有用IO前须要先打开其时钟线,下面是验证过oK的程序: RCC->APB2ENR|=GpioBApb2enrEn; //使能PORTB时钟 GP ...