[leetcode] 355. Design Twitter 解题报告

8 篇文章 0 订阅
6 篇文章 0 订阅

题目链接: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:

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. 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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. 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);
 */


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值