当前位置: 首页 > news >正文

leetcode 355 设计推特

用链表存储用户发送的每一个推特,用堆获取最先的10条动态

class Twitter {Map<Integer,Set<Integer>> followMap;//规定最新的放到最后Map<Integer,Tweet> postMap;//优先队列(堆)PriorityQueue<Tweet> priorityQueue;int timeStamp = 0;int limit = 10;public Twitter() {followMap = new HashMap();postMap = new HashMap<>();//按照每一个推特发送的时间戳由大到小排布priorityQueue = new PriorityQueue<>((t1,t2) -> t2.timeStamp - t1.timeStamp);}//userId发送推特public void postTweet(int userId, int tweetId) {//首先根据postMap来获取userId对应发送到文章Tweet tweet = postMap.get(userId);//生成新的tweetTweet newTweet = new Tweet(tweetId, timeStamp++, tweet);postMap.put(userId,newTweet);}//根据userId获得自己和关注用户的10条推特,按时间顺序由近到远排序public List<Integer> getNewsFeed(int userId) {//因为每一个用户都有自己的优先队列,所以先清空优先队列priorityQueue.clear();//将自己和关注的用户发送的最新的推特id先放入到优先队列if (postMap.containsKey(userId))priorityQueue.offer(postMap.get(userId));Set<Integer> follows = followMap.get(userId);if (follows != null){for (Integer follow : follows) {if (postMap.containsKey(follow))priorityQueue.offer(postMap.get(follow));}}//现在用户和所有关注的推特都已经放入到优先队列,开始获取前10条int count = 0;ArrayList<Integer> result = new ArrayList<>();while (!priorityQueue.isEmpty() && count < limit){//获取头部,在优先队列中删除Tweet tweet = priorityQueue.poll();result.add(tweet.id);if (tweet.next != null)priorityQueue.offer(tweet.next);count++;}return result;}//关注public void follow(int followerId, int followeeId) {// 被关注人不能是自己if (followeeId == followerId) {return;}Set<Integer> follows = followMap.getOrDefault(followerId, new HashSet<>());follows.add(followeeId);followMap.put(followerId,follows);}//取关public void unfollow(int followerId, int followeeId) {// 被关注人不能是自己if (followeeId == followerId) {return;}Set<Integer> follows = followMap.getOrDefault(followerId, new HashSet<>());follows.remove(followeeId);followMap.put(followerId,follows);}
}
class Tweet{int id;int timeStamp;Tweet next;public Tweet(int id, int timeStamp) {this.id = id;this.timeStamp = timeStamp;}public Tweet(int id, int timeStamp, Tweet next) {this.id = id;this.timeStamp = timeStamp;this.next = next;}
}/*** 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);*/

http://www.lryc.cn/news/143031.html

相关文章:

  • 倒数 2 周|期待 2023 Google开发者大会
  • 代码随想录day57
  • YOLOv5、v8改进:CrissCrossAttention注意力机制
  • RabbitMQ特性介绍和使用案例
  • Ansible 使用 RHEL 系统角色
  • 重新认识Android中的线程
  • 前端(十五)——GitHub开源一个react封装的图片预览组件
  • DELL Power Edge R740 安装 OracleLinux-R7-U9-Server
  • 深入了解OpenStack:创建定制化QCOW2格式镜像的完全指南
  • 【Java 中级】一文精通 Spring MVC - 数据格式化器(六)
  • Linux内核学习(十二)—— 页高速缓存和页回写(基于Linux 2.6内核)
  • 大数据-玩转数据-Flink窗口函数
  • Docker网络-探索容器网络如何相互通信
  • ESP32-CAM模块Arduino环境搭建测试
  • webassembly001 webassembly简述
  • vue 使用C-Lodop打印小票
  • 【C++进阶(二)】STL大法--vector的深度剖析以及模拟实现
  • 1. import pandas as pd 导入库
  • DMK5框选变量之后不显示其他位置的此变量高亮
  • 0061__Appium
  • 【DEVOPS】需求跟踪管理全面落地
  • 算法修炼Day57|647. 回文子串 ● 516.最长回文子序列
  • 呈现数据的精妙之道:选择合适的可视化方法
  • 数据结构(Java实现)-java对象的比较
  • Wolfram Mathematica 13 for Mac 数学计算工具
  • 系统架构设计高级技能 · Web架构
  • 再写CentOS7升级OpenSSL-1.0.1U
  • HBase--技术文档--基本概念--《快速扫盲》
  • 如何利用SFTP协议远程实现更安全的文件传输 ——【内网穿透】
  • 深度学习8:详解生成对抗网络原理