题目链接:https://leetcode.com/problems/design-twitter/
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.
Example:
Twitter twitter = new Twitter(); // User 1 posts a new tweet (id = 5). twitter.postTweet(1, 5); // User 1's news feed should return a list with 1 tweet id -> [5]. twitter.getNewsFeed(1); // User 1 follows user 2. twitter.follow(1, 2); // User 2 posts a new tweet (id = 6). twitter.postTweet(2, 6); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. // Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5. twitter.getNewsFeed(1); // User 1 unfollows user 2. twitter.unfollow(1, 2); // User 1's news feed should return a list with 1 tweet id -> [5], // since user 1 is no longer following user 2. twitter.getNewsFeed(1)
思路:可以利用一个hash表来保存每个用户信息,一个用户结构体应该包含follow的人列表,发布的消息,另外消息最好设计为一个list,这样每次消息插在头部.
1.follow函数需要注意的是不能follow自己
2.unfollow的时候判断一下之前有没有follow这个人
3.posttwitter的时候要带上时间戳
4.feedNews比较麻烦,可以用一个优先队列,类似于取k个有序链表中最小的n个数.
代码如下:
class Twitter {
public:
/** Initialize your data structure here. */
Twitter() {
}
struct User
{
unordered_set<int> followee;
list<pair<int,int>> news;
};
struct CMP
{
bool operator()(pair<list<pair<int, int>>::iterator, list<pair<int, int>>::iterator> a,
pair<list<pair<int, int>>::iterator, list<pair<int, int>>::iterator> b)
{
return a.first->second < b.first->second;
}
};
/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
hash[userId].news.insert(hash[userId].news.begin(), make_pair(tweetId, curTime++));
}
/** 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<list<pair<int, int>>::iterator, list<pair<int, int>>::iterator>,
vector<pair<list<pair<int, int>>::iterator, list<pair<int, int>>::iterator>>, CMP> que;
vector<int> news;
if(hash[userId].news.size() > 0)
que.push(make_pair(hash[userId].news.begin(), hash[userId].news.end()));
for(auto val: hash[userId].followee)
{
if(hash[val].news.size()==0) continue;
que.push(make_pair(hash[val].news.begin(), hash[val].news.end()));
}
int k = 10;
while(que.size() > 0 && k-- > 0)
{
auto val = que.top(); que.pop();
news.push_back(val.first->first);
if(++val.first != val.second) que.push(val);
}
return news;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
if(followerId == followeeId) return;
hash[followerId].followee.insert(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
if(hash[followerId].followee.count(followeeId)==0) return;
hash[followerId].followee.erase(hash[followerId].followee.find(followeeId));
}
private:
unordered_map<int, User> hash;
int curTime = 0;
};
/**
* 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);
*/