355. Design Twitter
来源:互联网 发布:vb二级考试题库 编辑:程序博客网 时间:2024/05/16 11:49
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods:
postTweet(userId, tweetId): Compose a new tweet.
getNewsFeed(userId): 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.
follow(followerId, followeeId): Follower follows a followee.
unfollow(followerId, followeeId): Follower unfollows a followee.
class Twitter {private: unordered_map<int,set<int>>followee; unordered_map<int, vector<pair<int, int>>>tweet; int count;public: /** Initialize your data structure here. */ Twitter() { count = 0; } /** Compose a new tweet. */ void postTweet(int userId, int tweetId) { tweet[userId].push_back({count++, 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) { priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> sorttweets; followee[userId].insert(userId); for(set<int>::iterator it=followee[userId].begin(); it!=followee[userId].end();++it) for(vector<pair<int,int>>::reverse_iterator rit=tweet[*it].rbegin(); rit!=tweet[*it].rend(); ++rit) { if(sorttweets.empty()) { sorttweets.push(*rit); } else { if(rit->first < sorttweets.top().first && sorttweets.size() == 10 ) { break; } sorttweets.push(*rit); if(sorttweets.size()>10) { sorttweets.pop(); } } } vector<int> res; while(!sorttweets.empty()) { res.push_back(sorttweets.top().second); sorttweets.pop(); } reverse(res.begin(), res.end()); followee[userId].erase(userId); return res; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ void follow(int followerId, int followeeId) { if(followerId != followeeId) { followee[followerId].insert(followeeId); } } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ void unfollow(int followerId, int followeeId) { if (followerId != followeeId) { followee[followerId].erase(followeeId); } }};/** * 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); */
0 0
- [leetcode] 355. Design Twitter
- 【LeetCode】355. Design Twitter
- leetcode.355. Design Twitter
- 355. Design Twitter
- 355. Design Twitter
- 355. Design Twitter
- [leetcode]355. Design Twitter
- 355. Design Twitter
- 355. Design Twitter
- 355. Design Twitter(unsolved)
- Leetcode 355. Design Twitter
- leetcode 355. Design Twitter
- leetcode 355. Design Twitter
- leetcode 355. Design Twitter
- 355. Design Twitter
- LeetCode 355. Design Twitter(设计Twitter)
- LeetCode—355. Design Twitter
- leetcode题解-355. Design Twitter
- Android监听ScrollView滑动到顶端和底部
- Java注解提供了关于代码的一些信息
- MYSQL---数据库中表的操作
- java中向上转型和向下转型
- C语言基础 产生一个不会变化的随机数
- 355. Design Twitter
- ubuntu FastDFS 安装 配置
- 3个管脚的HC-SR501的使用。
- java基础(三)方法/数组/堆栈/
- Linux学习之关机重启命令
- Iterator模式进行简单的说明
- linux线程的简介
- 搭起Java开发环境
- jmap命令