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

力扣355.设计推特

力扣355.设计推特

  • 设计Node类有哈希集合存关注人的Id,有链表存推文的顺序

    • 一个哈希表存推文的时间
  •   class Twitter {struct Node{unordered_set<int> followee;list<int> tweet;};int recentMax,time;unordered_map<int,int> tweettime;unordered_map<int,Node> user;public:Twitter() {//当前时间time = 0;//最大推文数recentMax = 10;user.clear();}void postTweet(int userId, int tweetId) {  if(user[userId].tweet.size() == recentMax)user[userId].tweet.pop_back();user[userId].tweet.push_front(tweetId);tweettime[tweetId] = ++time;}vector<int> getNewsFeed(int userId) {vector<int> res;for(auto it = user[userId].tweet.begin();it != user[userId].tweet.end();it++)res.emplace_back(*it);//遍历每个关注人 合并链表for(int followeeId : user[userId].followee){if(followeeId == userId) continue;vector<int> ans;auto it = user[followeeId].tweet.begin();int i = 0;while(i<(int)res.size() && it != user[followeeId].tweet.end()){if(tweettime[(*it)] > tweettime[res[i]]){ans.emplace_back(*it);it++;}else{ans.emplace_back(res[i]);i++;}if((int)ans.size() == recentMax) break;}//如果数量不够 并且res还有剩余for(;i<(int)res.size() && (int)ans.size() < recentMax;i++)ans.emplace_back(res[i]);//如果数量不够 并且关注人还有剩余for(;it != user[followeeId].tweet.end() && (int)ans.size() < recentMax;it++)ans.emplace_back(*it);//ans赋给resres.assign(ans.begin(),ans.end());}return res;}void follow(int followerId, int followeeId) {user[followerId].followee.insert(followeeId);}void unfollow(int followerId, int followeeId) {user[followerId].followee.erase(followeeId);}};
    

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 谷粒商城实战笔记-43-前端基础-Vue-使用Vue脚手架进行模块化开发
  • 【人工智能】Transformers之Pipeline(五):深度估计(depth-estimation)
  • 第三篇 Vue项目目录结构介绍
  • (35)远程识别(又称无人机识别)(二)
  • Java后端开发岗位面试经验总结:技术深度与实践能力的考验
  • CSS(二)——CSS 背景
  • 【C++】:AVL树的深度解析及其实现
  • 【YashanDB知识库】filter or改写问题
  • EXO项目解析:pynvml怎么实现监控的,包括什么参数
  • 怎么服务器组内网?
  • 【BES2500x系列 -- RTX5操作系统】系统启动流程 -- boot loader概念讲解 --(九)
  • GO内存分配详解
  • Python 使用TCP\UDP协议创建一个聊天室
  • 四、Object 通⽤⽅法
  • es master 节点数据丢失导致数据节点加入集群失败的灾难恢复
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • Android交互
  • codis proxy处理流程
  • export和import的用法总结
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • java中具有继承关系的类及其对象初始化顺序
  • JS 面试题总结
  • Kibana配置logstash,报表一体化
  • Next.js之基础概念(二)
  • React 快速上手 - 07 前端路由 react-router
  • supervisor 永不挂掉的进程 安装以及使用
  • Vue全家桶实现一个Web App
  • 工程优化暨babel升级小记
  • 配置 PM2 实现代码自动发布
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 用Python写一份独特的元宵节祝福
  • hi-nginx-1.3.4编译安装
  • RDS-Mysql 物理备份恢复到本地数据库上
  • ​520就是要宠粉,你的心头书我买单
  • ​必胜客礼品卡回收多少钱,回收平台哪家好
  • # 飞书APP集成平台-数字化落地
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (1)无线电失控保护(二)
  • (11)MATLAB PCA+SVM 人脸识别
  • (5)STL算法之复制
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • *** 2003
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .NET 5种线程安全集合
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET Reactor简单使用教程
  • .Net Winform开发笔记(一)
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET 设计一套高性能的弱事件机制
  • .NET 依赖注入和配置系统
  • .sh
  • @RequestMapping 和 @GetMapping等子注解的区别及其用法