【二维区域和检索-矩阵不可变】python刷题记录

一夜无解,痛苦,遂拜倒于灵神门下,妄做狂徒也!

. - 力扣(LeetCode)

灵神秒解如下:

python 复制代码
class NumMatrix:
    #二维初始矩阵
    def __init__(self, matrix: List[List[int]]):
       m=len(matrix)
       n=len(matrix[0])
       #扩充一下,防止数组越界
       sum=[[0]*(n+1) for _ in range(m+1)]
       for i in range(m):
           for j in range(n):
             sum[i+1][j+1]=sum[i+1][j]+sum[i][j+1]-sum[i][j]+matrix[i][j]
       self.sum=sum

    #子矩阵计算和
    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
       return self.sum[row2+1][col2+1]-self.sum[row2+1][col1]-self.sum[row1][col2+1]+self.sum[row1][col1]
    
相关推荐
JieE21210 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
金銀銅鐵15 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1119 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0021 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 天前
Agent 记忆管理
后端·python·agent