提交 8c45dca9 编写于 作者: 每日一练社区's avatar 每日一练社区

update exercises

上级 95b13843
......@@ -307,55 +307,44 @@ private:
### C
```c
class Twitter
{
class Twitter {
public:
unordered_map<int,unordered_set<int>> user;
unordered_map<int,int> tweetUser;
vector<int> list;
/** Initialize your data structure here. */
Twitter()
{
}
Twitter() {
}
/** Compose a new tweet. */
void postTweet(int userId, int tweetId)
{
context.push_back(make_pair(userId, tweetId));
void postTweet(int userId, int tweetId) {
list.push_back(tweetId);
tweetUser[tweetId] = 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. */
vector<int> getNewsFeed(int userId)
{
int n = context.size();
int count = 0;
vector<int> res;
int k = n - 1;
while (count < 10 && k >= 0)
{
auto it = context[k];
if (it.first == userId || tmp[make_pair(userId, it.first)])
{
res.push_back(context[k].second);
count++;
vector<int> getNewsFeed(int userId) {
vector<int> result;
int num=0;
for(int i=list.size()-1; i>=0&&num<10; i--){
if(tweetUser[list[i]]==userId || user[userId].find(tweetUser[list[i]]) != user[userId].end()){ //第一个条件是判断是不是自己发送的推特,第二个条件是判断是不是关注的人发送的推特
result.push_back(list[i]);
num++;
}
k--;
}
return res;
return result;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId)
{
tmp[make_pair(followerId, followeeId)] = 1;
void follow(int followerId, int followeeId) {
user[followerId].insert(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId)
{
tmp[make_pair(followerId, followeeId)] = 0;
void unfollow(int followerId, int followeeId) {
user[followerId].erase(followeeId);
}
private:
map<pair<int, int>, int> tmp;
vector<pair<int, int>> context;
};
/**
......@@ -366,5 +355,4 @@ private:
* obj->follow(followerId,followeeId);
* obj->unfollow(followerId,followeeId);
*/
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册