{"id":611,"date":"2020-09-26T11:03:13","date_gmt":"2020-09-26T03:03:13","guid":{"rendered":"http:\/\/39.96.58.60\/?p=611"},"modified":"2022-10-18T16:45:04","modified_gmt":"2022-10-18T08:45:04","slug":"leetcode-304-%e4%ba%8c%e7%bb%b4%e5%8c%ba%e5%9f%9f%e5%92%8c%e6%a3%80%e7%b4%a2-%e7%9f%a9%e9%98%b5%e4%b8%8d%e5%8f%af%e5%8f%98","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=611","title":{"rendered":"Leetcode 304. \u4e8c\u7ef4\u533a\u57df\u548c\u68c0\u7d22 &#8211; \u77e9\u9635\u4e0d\u53ef\u53d8"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff0c\u8ba1\u7b97\u5176\u5b50\u77e9\u5f62\u8303\u56f4\u5185\u5143\u7d20\u7684\u603b\u548c\uff0c\u8be5\u5b50\u77e9\u9635\u7684\u5de6\u4e0a\u89d2\u4e3a (<em>row<\/em>1,&nbsp;<em>col<\/em>1) \uff0c\u53f3\u4e0b\u89d2\u4e3a (<em>row<\/em>2,&nbsp;<em>col<\/em>2)\u3002<\/p>\n<p><img alt=\"Range Sum Query 2D\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/images\/304.png\" style=\"width: 130px;\"><\/p>\n<p><small>\u4e0a\u56fe\u5b50\u77e9\u9635\u5de6\u4e0a\u89d2&nbsp;(row1, col1) = <strong>(2, 1)<\/strong>&nbsp;\uff0c\u53f3\u4e0b\u89d2(row2, col2) = <strong>(4, 3)\uff0c<\/strong>\u8be5\u5b50\u77e9\u5f62\u5185\u5143\u7d20\u7684\u603b\u548c\u4e3a 8\u3002<\/small><\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<pre>\u7ed9\u5b9a matrix = [\n\n  [3, 0, 1, 4, 2],\n\n  [5, 6, 3, 2, 1],\n\n  [1, 2, 0, 1, 5],\n\n  [4, 1, 0, 1, 7],\n\n  [1, 0, 3, 0, 5]\n\n]\n\n\n\nsumRegion(2, 1, 4, 3) -&gt; 8\n\nsumRegion(1, 1, 2, 2) -&gt; 11\n\nsumRegion(1, 2, 2, 4) -&gt; 12\n\n<\/pre>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ol>\n<li>\u4f60\u53ef\u4ee5\u5047\u8bbe\u77e9\u9635\u4e0d\u53ef\u53d8\u3002<\/li>\n<li>\u4f1a\u591a\u6b21\u8c03\u7528&nbsp;<em>sumRegion&nbsp;<\/em>\u65b9\u6cd5<em>\u3002<\/em><\/li>\n<li>\u4f60\u53ef\u4ee5\u5047\u8bbe&nbsp;<em>row<\/em>1 \u2264 <em>row<\/em>2 \u4e14&nbsp;<em>col<\/em>1 \u2264 <em>col<\/em>2\u3002<\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u52a8\u6001\u89c4\u5212\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a128 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8692.83% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a16.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8629.48% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\n    \u8ba1\u7b97 \u5de6\u4e0a\u89d2\u5230\u67d0\u4e00\u70b9\u7684 \u548c\n\n    \u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u548c\u65f6\uff0c \u7b49\u4e8e\u53f3\u4e0b\u89d2\u70b9\u5230\u539f\u70b9\u7684\u548c - \u4e0a\u4fa7 - \u5de6\u4fa7 + \u5de6\u4e0a\u89d2\u5230\u539f\u70b9\u7684\u548c\n\"\"\"\nclass NumMatrix:\n\n    def __init__(self, matrix: List[List[int]]):\n        if matrix and matrix[0]:\n            self.m = len(matrix)\n            self.n = len(matrix[0])\n\n            self.dp = [[0 for _ in range(self.n+1)] for _ in range(self.m+1)]\n\n            for i in range(1, self.m+1):\n                for j in range(1, self.n+1):\n                    self.dp[i][j] = self.dp[i-1][j] + self.dp[i][j-1] - self.dp[i-1][j-1] + matrix[i-1][j-1]\n\n\n    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -&gt; int:\n        if self.m == 0 or self.n == 0:\n            return 0\n        # print(self.dp)\n        row1, col1, row2, col2 = row1+1, col1+1, row2+1, col2+1\n        # print(self.dp[row2][col2], self.dp[row1-1][col1], self.dp[row1][col1-1], self.dp[row1-1][col1-1])\n        return self.dp[row2][col2] - self.dp[row1-1][col2] - self.dp[row2][col1-1] + self.dp[row1-1][col1-1]\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff0c\u8ba1\u7b97\u5176\u5b50\u77e9\u5f62\u8303\u56f4\u5185\u5143\u7d20\u7684\u603b\u548c\uff0c\u8be5\u5b50\u77e9\u9635\u7684\u5de6\u4e0a\u89d2\u4e3a (row1,&nbsp;col1) \uff0c\u53f3\u4e0b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/611"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=611"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/611\/revisions"}],"predecessor-version":[{"id":612,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/611\/revisions\/612"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=611"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}