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

[Lc_week] 447 | 155 | Q1 | hash | pair {}调用

447_Q1

题解

class Solution {typedef pair<int,int> PII;// 自定义哈希函数struct HashPII {size_t operator()(const PII& p) const {return hash<int>()(p.first) ^ (hash<int>()(p.second) << 1);}};public:int countCoveredBuildings(int n, vector<vector<int>>& buildings) {//不管有多远 至少存在一个建筑//bfs变形//哦不对,hash 快速查找同行同列,不就可以判断了吗//添加自定义哈希函数HashPII,使用unordered_map<PII, bool, HashPII>解决pair作为键的问题unordered_map<PII,bool,HashPII> hash;int ans=0;for(auto& b:buildings){PII p={b[0],b[1]};  //pair构造和调用 要用{}hash[p]=true;}for(auto& b:buildings){int cnt=0;//四个 方向的查找//上for(int i=b[1]+1;i<=n;i++){if(hash.count({b[0],i})){cnt++;break;}}for(int i=b[1]-1;i>=1;i--){if(hash.count({b[0],i})){cnt++;break;}}for(int i=b[0]-1;i>=1;i--){if(hash.count({i,b[1]})){cnt++;break;}}for(int i=b[0]+1;i<=n;i++){if(hash.count({i,b[1]})){cnt++;break;}}if(cnt==4) ans++;}return ans;}
};©leetcode
  • pair 构造和调用 要用 {}
  • !!!!!!!!!!!!!!花括号

超时了

优化

  • 采取 行列极值法
class Solution {
public:int countCoveredBuildings(int n, vector<vector<int>>& buildings) {// 存 行位置极值和列位置极值unordered_map<int, int> row_min, row_max;unordered_map<int, int> col_min, col_max;for (auto& b : buildings) {int x = b[0], y = b[1];// 预处理行极值if (!row_min.count(x) || y < row_min[x])row_min[x] = y;if (!row_max.count(x) || y > row_max[x])row_max[x] = y;// 预处理列极值if (!col_min.count(y) || x < col_min[y])col_min[y] = x;if (!col_max.count(y) || x > col_max[y])col_max[y] = x;}int ans = 0;for (auto& b : buildings) {int x = b[0], y = b[1];bool up = (y < row_max[x]);bool down = (y > row_min[x]);bool left = (x > col_min[y]);bool right = (x < col_max[y]);if (up && down && left && right)ans++;}return ans;}
};©leetcode

155_Q1

class Solution {
public:string findCommonResponse(vector<vector<string>>& responses) {unordered_map<string,int> hash;for(auto& re:responses){unordered_map<string,bool> check;for(auto& str:re){if(!check[str]){check[str]=true;hash[str]++; //确保 每一个 只加一次}}}pair<string,int> ret(responses[0][0],0);for(auto& [a,b]:hash){string& f=ret.first;int& g=ret.second;if(b==g){if(a<f)f=a;}if(b>g){f=a;g=b;}}return ret.first;}
};
http://www.xdnf.cn/news/2351.html

相关文章:

  • 【Linux网络】Http服务优化 - 增加请求后缀、状态码描述、重定向、自动跳转及注册多功能服务
  • 51单片机所有寄存器介绍
  • FFmpeg之三 录制音频并保存, API编解码从理论到实战
  • Linux红帽:RHCSA认证知识讲解(十 四)分区管理、交换分区,创建逻辑卷与调整逻辑卷的大小
  • Spark Streaming实时数据处理实战:从DStream基础到自定义数据源集成
  • FPGA基础之基础语法
  • Unreal Engine 实现智慧水库周边环境以及智慧社区模拟的实例
  • Flutter 学习之旅 之 flutter 有时候部分手机【TextField】无法唤起【输入法软键盘】的一些简单整理
  • LINUX的使用(2)- 安装软件
  • Comfy UI 笔记
  • Selenium自动化测试+OCR-获取图片页面小说
  • 入职学习记录-RT-thread实时操作系统_1
  • Android HAL HIDL
  • C语言:数据的存储
  • matplotlib画图工具使用(1) 画折线统计图python代码
  • 【教学类-102-19】蝴蝶三色图作品1——卡纸蝴蝶(滴颜料按压对称花纹)A4横版最大号22.85CM
  • 如何在学习通快速输入答案(网页版),其他学习平台通用,手机上快速粘贴
  • 时间序列成像之点对称模式(Symmetrized Dot Pattern,SDP)
  • 【2025 最新前沿 MCP 教程 04】通信渠道:理解 MCP 传输机制
  • ShenNiusModularity项目源码学习(22:ShenNius.Admin.Mvc项目分析-7)
  • 【深度强化学习 DRL 快速实践】稀疏奖励问题 sparse reward
  • 高中数学联赛模拟试题精选第17套几何题
  • C++中const的应用(1)之const 形参和实参以及成员函数的 const 和重载
  • 从循环角度分析逐位分离法
  • 形象的讲解什么是Gateway网关
  • leetcode110 平衡二叉树
  • Ubuntu 之软件包管理系统
  • 前端技术个人求职简历模板
  • new/delete 重载与对象池实现
  • 高性能电脑系统优化工具Advanced SystemCare PRO v18.3.0.240 解锁永久专业版