leetcode-355-设计推特

在这里插入图片描述

import java.util.*;
class Twitter {

    private static int timestamp = 0;

    // userId 和 User 对象用一个map一一对应
    private HashMap<Integer, User> userMap;

    private static class Tweet {
        private int id;
        private int time;
        // 指针指向上一条推文
        private Tweet next;

        // 参数分别代表推文内容和发表时间
        public Tweet(int id, int time) {
            this.id = id;
            this.time = time;
            this.next = null;
        }
    }

    private static class User {
        private int id;
        // 关注列表不可重复 用set存储
        public Set<Integer> followed;
        // 推文列表用链表存储 便于有序合并
        public Tweet head;

        public User(int userId) {
            followed = new HashSet<>();
            this.id = userId;
            this.head = null;
            // 关注自己
            follow(id);
        }

        public void follow(int userId) {
            followed.add(userId);
        }

        public void unfollow(int userId) {
            // 不可取关自己
            if (userId != this.id) {
                followed.remove(userId);
            }
        }

        public void post(int tweetId) {
            Tweet tweet = new Tweet(tweetId, timestamp);
            timestamp++;
            // 新发表的推文插入推文链表头部
            tweet.next = head;
            head = tweet;
        }
    }

    /** Initialize your data structure here. */
    public Twitter() {
        userMap = new HashMap<>();
    }
    
    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        // 新用户第一次发推 建立 userId 和 User 对象映射关系
        if (!userMap.containsKey(userId)) {
            userMap.put(userId, new User(userId));
        }
        User user = userMap.get(userId);
        user.post(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. */
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> ans = new ArrayList<>();
        // 既没关注任何人 也没发推文 或者不存在
        if (!userMap.containsKey(userId)) {
            return ans;
        }
        // 关注列表
        Set<Integer> users = userMap.get(userId).followed;
        // lambda表达式 -> 通过time降序排列,队列大小为关注列表的大小
        PriorityQueue<Tweet> pq = new PriorityQueue<>(users.size(), (a,b) -> (b.time - a.time));
        // 将关注列表里所有头节点插入优先级队列
        for (int id : users) {
            Tweet tweet = userMap.get(id).head;
            // 关注的人未发推 
            if (tweet == null) {
                continue;
            }
            pq.add(tweet);
        }

        while (!pq.isEmpty()) {
            // 最多返回10条
            if (ans.size() == 10) {
                break;
            }
            // 取出 time 最大 即最新的推文
            Tweet tweet = pq.poll();
            ans.add(tweet.id);
            // 将下一篇推文插入队列排序
            if (tweet.next != null) {
                pq.add(tweet.next);
            }
        }
        return ans;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        // follower 第一次关注 建立 followerId 和 User 对象映射关系
        if (!userMap.containsKey(followerId)) {
            userMap.put(followerId, new User(followerId));
        }
        // // followee 第一次被关注 建立 followeeId 和 User 对象映射关系
        if (!userMap.containsKey(followeeId)) {
            userMap.put(followeeId, new User(followeeId));
        }
        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) {
        if (userMap.containsKey(followerId)) {
            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);
 */

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值