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. HBase错误:ERROR: Can't get master address from ZooKeeper; znode data == null 解决办法

    一.问题背景 使用命令 $ hbase shell 进入hbase的shell之后使用create命令创建表时出现错误:ERROR: Can't get master address from Zoo ...

  2. Ionic 自动创建应用的图标与启动画面

    你只需要一个目录和两张图片就可以搞定.图片可以是 .png 的,Photoshop的 .psd,或者Illustrator的 .ai,(例如)命名为icon.png和splash.png.把这些图片放 ...

  3. Scala入门3(特质线性化)

    尝试设计一套特质,灵活的改动整数队列.队列有两种操作:put把整数放入队列,get从尾部取出它们.队列是先进先出的,get应该依照入队列的顺序取数据.提示:可以用mutable.ArrayBuffer ...

  4. firewalld启动问题

    问题 在查看或启动firewalld服务时,提示"Warning: firewalld.service changed on disk. Run 'systemctl daemon-relo ...

  5. 组装者模式在React Native项目中的一个实战案例

    前言 在实际的开发中,如果遇到多个组件有一些共性,我们可以提取一个BaseItem出来,然后在多个组件中进行复用,一种方式是通过继承的方式,而今天我们要说的是另一种方式--组装者模式. 什么是组装者模 ...

  6. Python中 *args 和 **kwargs 的区别

    先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '----------- ...

  7. Linux进程管理子系统

    <进程要素> <进程与程序的区别> 程序: 存放在硬盘上一些列代码和数据的可执行映像,是一个静止的实体 进程: 是一个执行中的程序,是动态的实体 <进程4要素> 1 ...

  8. wx小程序碎碎念

    对button组件的例子中,js代码的一点理解 for (var i = 0; i < types.length; ++i) { (function(type) { // 循环构建目标函数 pa ...

  9. Splay 模板

    Splay 模板 struct SplayTree{ const static int maxn = 1e5 + 15; int ch[maxn][2] , key[maxn] , s[maxn] , ...

  10. HDU 1754 I Hate It 线段树RMQ

    I Hate It Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=175 ...