ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

355. 设计推特

2021-06-29 23:31:25  阅读:198  来源: 互联网

标签:推特 followeeId userMap int userId 355 设计 followerId 推文


算法题(程序员面试宝典)

解题思路主要来源于leetcode官方与《程序员面试宝典》&labuladong

355. 设计推特

设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能:

postTweet(userId, tweetId): 创建一条新的推文
getNewsFeed(userId): 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。推文必须按照时间顺序由最近的开始排序。
follow(followerId, followeeId): 关注一个用户
unfollow(followerId, followeeId): 取消关注一个用户
示例:

Twitter twitter = new Twitter();

// 用户1发送了一条新推文 (用户id = 1, 推文id = 5).
twitter.postTweet(1, 5);

// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
twitter.getNewsFeed(1);

// 用户1关注了用户2.
twitter.follow(1, 2);

// 用户2发送了一个新推文 (推文id = 6).
twitter.postTweet(2, 6);

// 用户1的获取推文应当返回一个列表,其中包含两个推文,id分别为 -> [6, 5].
// 推文id6应当在推文id5之前,因为它是在5之后发送的.
twitter.getNewsFeed(1);

// 用户1取消关注了用户2.
twitter.unfollow(1, 2);

// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
// 因为用户1已经不再关注用户2.
twitter.getNewsFeed(1);

解题方法

解题思路1

class Twitter {

    //时间戳
    private static int timestamp=0;
    //推特结构
    private static class Tweet{
        private int tweetId;
        private int time;
        private Tweet next;

        public Tweet(int tweetId){
            this.tweetId = tweetId;
            this.time = timestamp;
            //更新时间戳
            timestamp++;
            this.next = null;
        }
    }

    private static class User{
        //用户Id
        private int userId;
        //关注的用户,不重复
        private HashSet<Integer> followers;
        //当前用户的推特链表头
        private Tweet head;

        public User(int userId){
            this.userId = userId;
            this.followers = new HashSet<>();
            //将自身添加至关注者列表中
            this.followers.add(userId);
            this.head = null;
        }

        public void follow(int follower){
            this.followers.add(follower);
        }

        public void unfollow(int follower){
            //不可取消关注自己
            if(this.userId!=follower)
                this.followers.remove(follower);
        }

        public void postTweet(Tweet tweet){
            //头插法 将大的时间戳排在前
            tweet.next = head;
            head = tweet;
        }
    }

    //userId 到 用户信息的映射
    HashMap<Integer,User> userMap;
    //采用优先级队列 以tweet的时间戳为依据 对tweet进行排序
    PriorityQueue<Tweet> q;
    /** Initialize your data structure here. */
    public Twitter() {
        this.userMap = new HashMap<>();
    }
    
    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        User user;
        if(!this.userMap.containsKey(userId)){
            //当前用户不存在,先创建用户
            user = new User(userId);
            this.userMap.put(userId,user);
        }

        user = this.userMap.get(userId);
        Tweet tweet = new Tweet(tweetId);
        user.postTweet(tweet);
    }
    
    /** 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. */
    public List<Integer> getNewsFeed(int userId) {
        //获取当前用户的
        ArrayList<Integer> res = new ArrayList<>();
        if(!this.userMap.containsKey(userId)){
            this.userMap.put(userId,new User(userId));
        }
        User user = this.userMap.get(userId);
        //按照时间的顺序大小进行排序
        this.q = new PriorityQueue<>(user.followers.size(),(a,b)->{return b.time-a.time;});
        for(Integer u:user.followers){
            //将每一个关注的用户的tweet的head 加入q中
            if(this.userMap.get(u).head!=null)
                this.q.add(this.userMap.get(u).head);
        }
        //将时间戳大的加入要返回的res
        while(!q.isEmpty()){
            if(res.size()==10)
                break;
            Tweet tweet = this.q.poll();
            res.add(tweet.tweetId);
            if(tweet.next!=null){
                this.q.add(tweet.next);
            }
        }
        return res;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        //followerId 不存在 则新建
        if(!this.userMap.containsKey(followerId)){
            this.userMap.put(followerId,new User(followerId));
        }
        //followeeId 不存在 则新建
        if(!this.userMap.containsKey(followeeId)){
            this.userMap.put(followeeId,new User(followeeId));
        }
        this.userMap.get(followerId).follow(followeeId);
    }
    
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        //followerId 不存在 则新建
        if(!this.userMap.containsKey(followerId)){
            this.userMap.put(followerId,new User(followerId));
        }
        //followeeId 不存在 则新建
        if(!this.userMap.containsKey(followeeId)){
            this.userMap.put(followeeId,new User(followeeId));
        }
        this.userMap.get(followerId).unfollow(followeeId);
    }
}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */

在这里插入图片描述

标签:推特,followeeId,userMap,int,userId,355,设计,followerId,推文
来源: https://blog.csdn.net/weixin_40764894/article/details/118346144

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有