leetcode刷题:304. 二维区域和检索 - 矩阵不可变

in hive-180932 •  2 years ago 

题目:

image.png

今天这题目算是昨天303的威力加强版,思路是按昨天的来,先计算好每个数组各种排列的索引元素的和,然后再稍微一减。最后把和相加。

代码如下:

class NumMatrix:
    def __init__(self, matrix: List[List[int]]):
        self.num_sum = []
        for i in matrix:
            cache=[0]
            for t in range(len(i)):
                cache.append(cache[t]+i[t])
            self.num_sum.append(cache)      
    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        re=0
        if row2-row1 >0:
            for i in range(row2-row1+1):
                re += self.num_sum[row1+i][col2+1]-self.num_sum[row1+i][col1]
        else:
            re = self.num_sum[row1][col2+1]-self.num_sum[row1][col1]
        return re

image.png

速度有点慢,我想想怎么改进

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

拍拍

拍拍

请教问题:我想投票nutbox作为见证人,为什么老是提示 Oops, something went wrong. Here is the error message:
"voter.proxy.size() == 0: A proxy is currently set, please clear the proxy before voting for a witness." ,

是因为uses maiyude as a voting proxy?