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. 关于谷歌浏览器62版本之后引用video.js不能自动播放的问题(Cross-origin plugin content from http://vjs.zencdn.net/swf/5.0.0-rc1/video-js.swf must have a visible size larger than 400 x 300 pixels, or it will be blocked.)

    Cross-origin plugin content from http://vjs.zencdn.net/swf/5.0.0-rc1/video-js.swf must have a visibl ...

  2. 预备作业02 : 体会做中学(Learning By Doing)

    1.你有什么技能比大多人(超过班级90%以上)更好? 如果说不算上玩玻璃球在诸如此类不登大雅之堂的技能,我想我是没有什么比大多数人更好的.我的兴趣还算广泛,但很多东西也只是学到了皮毛而已. 在我上初中 ...

  3. 理解URI

    ---恢复内容开始--- 参考 https://zh.wikipedia.org/wiki/%E7%BB%9F%E4%B8%80%E8%B5%84%E6%BA%90%E6%A0%87%E5%BF%97 ...

  4. [Arc058E] Iroha and Haiku

    [Arc058E] Iroha and Haiku 题目大意 问有多少\(n\)个数的正整数序列,每个数在\([1,10]\)之间,满足存在\(x,y,z,w\)使得\(x\to y-1,y\to z ...

  5. [CC-MCHEF]MasterChef

    [CC-MCHEF]MasterChef 题目大意: \(n(n\le10^5)\)片花,第\(i\)片花的美观度为\(b_i(|b_i|\le10^9)\).总体美观度为各片花的美观度之和. 由于有 ...

  6. python开发_tkinter_单选按钮

    这篇blog主要是描述python中tkinter的单选按钮操作 下面是我做的demo 运行效果: ====================================== 代码部分: ===== ...

  7. jmeter-分布式部署之负载机的设置

    本文分三个部分: 1.windows下负载机的配置 2.Linux下负载机的配置 3.遇到的问题 *************************************************** ...

  8. malloc、calloc和realloc比较

    1.先看看它们的原型(stdlib.h): void *malloc( size_t size ); void *calloc( size_t numElements, size_t sizeOfEl ...

  9. jquery 图片预加载

    /图片无序预加载 (function($){ function Preload(imgs,fns){ this.imgs=(typeof imgs==="string")?[img ...

  10. mysql的show status详解(转载)

    SHOW STATUS提供服务器的状态信息(象mysqladmin extended-status一样).输出类似于下面的显示,尽管格式和数字可以有点不同: +-------------------- ...