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

倒序思想|hash

lc3817

枚举结尾索引

class Solution {
public:
vector<int> goodIndices(string s) {
vector<int> res;
for (int i = 0; i < s.length(); i++) {
string toMatch = to_string(i);
if (!s.compare(i + 1 - toMatch.length(), toMatch.length(), toMatch)) {
res.push_back(i);
}
}
return res;
}
};

lc3822

hash表的基本使用

struct Order {
int orderId, price;
string orderType;
Order(int oi, string ot, int p) : orderId(oi), orderType(ot), price(p) {};
};

class OrderManagementSystem {
unordered_map<int, Order*> map;
public:
OrderManagementSystem() {}

void addOrder(int orderId, string orderType, int price) {
Order* o = new Order(orderId, orderType, price);
map[orderId] = o;
}

void modifyOrder(int orderId, int newPrice) {
map[orderId]->price = newPrice;
}

void cancelOrder(int orderId) {
map.erase(orderId);
}

vector<int> getOrdersAtPrice(string orderType, int price) {
vector<int> ans;
for(const auto& p : map)
if(p.second->orderType == orderType && p.second->price == price)
ans.push_back(p.first);
return ans;
}
};

lc3807

二分+bfs

class Solution:
def minCost(self, n: int, edges: List[List[int]], k: int) -> int:
g = [[] for _ in range(n)]
for x, y, w in edges:
g[x].append((y, w))
g[y].append((x, w))

vis = [-1] * n
def check(mx: int) -> bool:
q = [0]
left = k
while q and left:
tmp = q
q = []
for x in tmp:
for y, w in g[x]:
if w > mx:
continue
if y == n - 1:
return True
if vis[y] != mx:
vis[y] = mx
q.append(y)
left -= 1
return False

mx = max(e[2] for e in edges)
ans = bisect_left(range(mx + 1), True, key=check)
return -1 if ans > mx else ans

lc3817

hash计数+倒序遍历

class Solution {
public:
vector<int> delayedCount(vector<int>& nums, int k) {
int n = nums.size(), counts[100001] = {0}; //[i+k+1, n-1]元素频率
vector<int> ans(n, 0);
for (int i = n - 1; i >= 0; --i) { //从后往前遍历
int j = i + k + 1; //进入有效统计范围的元素下标
if (j < n) counts[nums[j]]++; //哈希计数
ans[i] = counts[nums[i]]; //在i+k之后有相等的元素
}
return ans;
}
};

http://www.jsqmd.com/news/375658/

相关文章:

  • 提示工程架构师:打造高性能提示缓存机制的秘诀
  • 【YOLOv8多模态涨点改进】独家创新首发| CVPR 2025 | 引入FDSM频率域动态地选择模块,高效融合红外和可见光多模态特征,精准保留有用信息、抑制冗余与噪声,助力目标检测、图像分割、分类
  • AI提效神器|2026年6款宝藏PPT生成软件推荐,程序员/职场人速藏 - 品牌测评鉴赏家
  • 【YOLOv8多模态涨点改进】CVPR 2025 | 引入RLAB残差线性注意力块,有效融合并强调多尺度特征,多种创新改进点,助力多模态融合目标检测、图像分割、图像分类,医学图像分割等任务有效涨点
  • 大数据领域数据编目的最佳实践分享
  • Java毕设项目:基于springboot的陶瓷售卖系统(源码+文档,讲解、调试运行,定制等)
  • 告别熬夜做PPT!2026AI博主亲测6款AI PPT工具,效率拉满200% - 品牌测评鉴赏家
  • 【YOLOv11多模态涨点改进】独家创新首发| CVPR 2025 | 引入FDSM频率域动态地选择模块,高效融合红外和可见光多模态特征,精准保留有用信息、抑制冗余与噪声,助力目标检测、图像分割、分类
  • 爬虫基础
  • 【毕业设计】基于springboot的陶瓷售卖系统(源码+文档+远程调试,全bao定制等)
  • 【YOLOv11多模态涨点改进】独家复现创新首发 | CVPR 2025 | 引入 FEFM 频率穷举融合机制和二次创新CFEM交叉融合增强模块,适合红外与可见光融合,多模态融合目标检测、实例分割
  • 【YOLOv11多模态涨点改进】CVPR 2025 | 引入RLAB残差线性注意力块,有效融合并强调多尺度特征,多种创新改进点,助力多模态融合目标检测、图像分割、图像分类,医学图像分割等任务有效涨点
  • 2026国内最新汽车胶生产厂家TOP5推荐:服务深度交覆盖江苏、山东、济南、云南等地,汽车胶优质服务商权威榜单发布,多场景适配助力品质升级 - 品牌推荐2026
  • 完整教程:C#低功耗工控通信实战|MQTT-SN协议全解析 + 无线传感器网络(WSN)对接完整落地
  • 移动云政务智能体是做什么的?
  • RAG 时代的“破壁人”:为什么你的大模型应用急需 Docling?
  • Prometheus、Cadvisor和Grafana体系完整学习手册 - 实践
  • 《程序员修炼之道:从小工到专家》读后感
  • harmonyOS软件开发的开端——DevEcoStudio
  • 白银千年妖
  • LeetCode 3713.最长的平衡子串 I:计数(模拟)
  • P2293 学习笔记
  • 大模型——什么是Agent Skills 和MCP 有什么区别
  • 【预测模型】麻雀搜索算法优化ELMAN神经网络(SSA-ELMAN)的光伏功率预测附Matlab代码
  • 第2章 搭建第一个C语言学习环境-【 2.4 常见编译错误与解决(新手必读)】
  • 2026国内最新耐候胶厂商TOP5推荐:服务深度交覆盖江苏、山东、济南、云南等地,覆盖多场景的优质耐候胶品牌权威榜单,适配厨卫/门窗/全屋定制等多元需求 - 品牌推荐2026
  • 【预测模型】蜂群算法改进支持向量机(ABC-SVM)的融资风险评价附Matlab代码
  • 美妆博主实测|6款高端手动剃须刀推荐 精致男士必入! - 品牌测评鉴赏家
  • P1919 学习笔记
  • 系列报告十三:(MTB)Physical AI: Shaping the Market of the New Possible — 2025 Report - 实践