博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
design-twitter
阅读量:6848 次
发布时间:2019-06-26

本文共 2532 字,大约阅读时间需要 8 分钟。

https://leetcode.com/problems/design-twitter/class Twitter {    unordered_map
> follower_mp; unordered_map
> followee_mp; unordered_map
> > twitter_self_mp; int timestamp;public: /** Initialize your data structure here. */ Twitter() { timestamp = 0; } /** Compose a new tweet. */ void postTweet(int userId, int tweetId) { if (twitter_self_mp.find(userId) == twitter_self_mp.end()) { vector
> 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
getNewsFeed(int userId) { set
tmp_set; if (follower_mp.find(userId) != follower_mp.end()) { tmp_set = follower_mp[userId]; } tmp_set.insert(userId); vector
> result; set
::iterator itr = tmp_set.begin(); for (; itr != tmp_set.end(); ++itr) { if (twitter_self_mp.find(*itr) == twitter_self_mp.end()) { continue; } int curPos = 0; int curSize = result.size(); vector
> tmp_vec = twitter_self_mp[*itr]; vector
>::iterator vitr = tmp_vec.begin(); for (; vitr != tmp_vec.end() && curPos < 10; ++vitr) { while (curPos < 10 && curPos < curSize && result[curPos].first > vitr->first) { curPos++; } if (curPos >= 10) { break; } result.insert(result.begin() + curPos, *vitr); curPos++; curSize++; } } vector
ret; int rlen = result.size(); for (int i=0; i<10 && i
tmp_set; follower_mp[followerId] = tmp_set; } follower_mp[followerId].insert(followeeId); if (followee_mp.find(followeeId) == followee_mp.end()) { set
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
param_2 = obj.getNewsFeed(userId); * obj.follow(followerId,followeeId); * obj.unfollow(followerId,followeeId); */

 

转载地址:http://idrul.baihongyu.com/

你可能感兴趣的文章
Grunt + Bower—前端构建利器(转)
查看>>
xpath属性值的模糊匹配
查看>>
221. Maximal Square
查看>>
python --文本文件的输入输出
查看>>
Tslib的移植【转】
查看>>
iOS开发--音乐文件播放工具类的封装(包含了音效的封装)
查看>>
如何获取一个AlertDialog中的EditText中输入的内容
查看>>
OpenGL帧缓存对象(FBO:Frame Buffer Object) 【转】
查看>>
hihoCoder_二分&#183;归并排序之逆序对
查看>>
掩码计算工具netmask
查看>>
linux中内核的一个不错的参数somaxconn
查看>>
android自动更新软件版本
查看>>
Silverlight 引路蜂二维图形库示例:绘制各种几何图形
查看>>
浅谈HTTP中Get与Post的区别
查看>>
Sql建表和sql语句的注意事项
查看>>
asp(javascript) 判断是否post表单项
查看>>
Vector
查看>>
每日英语:Dating in China Is a Largely Commercial Transaction
查看>>
ziproxy 3.3.0 发布,HTTP代理服务器
查看>>
C++ 简单选择排序
查看>>