{"id":519,"date":"2020-09-26T10:51:26","date_gmt":"2020-09-26T02:51:26","guid":{"rendered":"http:\/\/39.96.58.60\/?p=519"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-1030-%e8%b7%9d%e7%a6%bb%e9%a1%ba%e5%ba%8f%e6%8e%92%e5%88%97%e7%9f%a9%e9%98%b5%e5%8d%95%e5%85%83%e6%a0%bc","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=519","title":{"rendered":"Leetcode 1030. \u8ddd\u79bb\u987a\u5e8f\u6392\u5217\u77e9\u9635\u5355\u5143\u683c"},"content":{"rendered":"<p>\u7ed9\u51fa <code>R<\/code> \u884c <code>C<\/code> \u5217\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u7684\u5355\u5143\u683c\u7684\u6574\u6570\u5750\u6807\u4e3a <code>(r, c)<\/code>\uff0c\u6ee1\u8db3 <code>0 &lt;= r &lt; R<\/code> \u4e14 <code>0 &lt;= c &lt; C<\/code>\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u6211\u4eec\u5728\u8be5\u77e9\u9635\u4e2d\u7ed9\u51fa\u4e86\u4e00\u4e2a\u5750\u6807\u4e3a&nbsp;<code>(r0, c0)<\/code> \u7684\u5355\u5143\u683c\u3002<\/p>\n<p>\u8fd4\u56de\u77e9\u9635\u4e2d\u7684\u6240\u6709\u5355\u5143\u683c\u7684\u5750\u6807\uff0c\u5e76\u6309\u5230 <code>(r0, c0)<\/code> \u7684\u8ddd\u79bb\u4ece\u6700\u5c0f\u5230\u6700\u5927\u7684\u987a\u5e8f\u6392\uff0c\u5176\u4e2d\uff0c\u4e24\u5355\u5143\u683c<code>(r1, c1)<\/code> \u548c <code>(r2, c2)<\/code> \u4e4b\u95f4\u7684\u8ddd\u79bb\u662f\u66fc\u54c8\u987f\u8ddd\u79bb\uff0c<code>|r1 - r2| + |c1 - c2|<\/code>\u3002\uff08\u4f60\u53ef\u4ee5\u6309\u4efb\u4f55\u6ee1\u8db3\u6b64\u6761\u4ef6\u7684\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002\uff09<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>R = 1, C = 2, r0 = 0, c0 = 0\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[0,0],[0,1]]\n\n<strong>\u89e3\u91ca<\/strong>\uff1a\u4ece (r0, c0) \u5230\u5176\u4ed6\u5355\u5143\u683c\u7684\u8ddd\u79bb\u4e3a\uff1a[0,1]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>R = 2, C = 2, r0 = 0, c0 = 1\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[0,1],[0,0],[1,1],[1,0]]\n\n<strong>\u89e3\u91ca<\/strong>\uff1a\u4ece (r0, c0) \u5230\u5176\u4ed6\u5355\u5143\u683c\u7684\u8ddd\u79bb\u4e3a\uff1a[0,1,1,2]\n\n[[0,1],[1,1],[0,0],[1,0]] \u4e5f\u4f1a\u88ab\u89c6\u4f5c\u6b63\u786e\u7b54\u6848\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>R = 2, C = 3, r0 = 1, c0 = 2\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]\n\n<strong>\u89e3\u91ca<\/strong>\uff1a\u4ece (r0, c0) \u5230\u5176\u4ed6\u5355\u5143\u683c\u7684\u8ddd\u79bb\u4e3a\uff1a[0,1,1,2,2,3]\n\n\u5176\u4ed6\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u7b54\u6848\u4e5f\u4f1a\u88ab\u89c6\u4e3a\u6b63\u786e\uff0c\u4f8b\u5982 [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]]\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= R &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= C &lt;= 100<\/code><\/li>\n<li><code>0 &lt;= r0 &lt; R<\/code><\/li>\n<li><code>0 &lt;= c0 &lt; C<\/code><\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6392\u5e8f\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\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a196 ms\n\u5185\u5b58\u6d88\u8017\uff1a15.6 MB\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5c06\u6240\u6709\u7684\u70b9\u5b58\u5728\u4e00\u4e2a\u5217\u8868\u91cc\u3002\n    \u5bf9\u5217\u8868\u6309\u8ddd\u79bb\u8fdb\u884c\u6392\u5e8f\u3002\n    \u6700\u7b80\u6d01\uff0c\u4f46\u4e5f\u6700\u6162\u3002\n\"\"\"\nclass Solution:\n    def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int) -&gt; List[List[int]]:\n        lists = [[r, c] for r in range(R) for c in range(C)]\n        lists = sorted(lists, key=lambda p: abs(p[0]-r0)+abs(p[1]-c0))\n        return lists\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a180 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8672.17% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a15.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e86100.00% \u7684\u7528\u6237\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5faa\u73af\u8ba1\u7b97\u5404\u70b9\u4e0e\u539f\u70b9\u8ddd\u79bb\uff0c\u5b58\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5c06\u540c\u8ddd\u79bb\u70b9\u90fd\u5b58\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5217\u8868\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e2d\u5b58\u50a8\u8ddd\u79bb\u4e3a1\u7684\u70b9\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4e2d\u5b58\u50a8\u8ddd\u79bb\u4e3a2\u7684\u70b9\u3002\n    \u904d\u5386\u5217\u8868\uff0c\u83b7\u53d6\u70b9\u3002\n\"\"\"\nclass Solution:\n    def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int):\n        dists = [[] for _ in range(R+C)]\n        for r in range(abs(R)):\n            for c in range(abs(C)):\n                dists[abs(r-r0)+abs(c-c0)].append([r, c])\n\n        result = []\n        for dist in dists:\n            if dist !=[]:\n                result.extend(dist)\n        return result\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a172 ms\n\u5185\u5b58\u6d88\u8017\uff1a15.3 MB\n\u89e3\u9898\u601d\u8def\uff1a\n    \u601d\u8def\u4e0e2\u76f8\u540c\uff0c\u4f46\u5c06\u5217\u8868\u6362\u6210\u5b57\u6bb5\u3002\n\"\"\"\nclass Solution:\n    def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int):\n        dists = {}\n        for r in range(abs(R)):\n            for c in range(abs(C)):\n                dist = abs(r-r0)+abs(c-c0)\n                if dist not in dists.keys():\n                    dists[dist] = []\n                dists[dist].append([r, c])\n        result = []\n        dists_keys = sorted(dists.keys())\n        for dist in dists_keys:\n            result.extend(dists[dist])\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u51fa R \u884c C \u5217\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u7684\u5355\u5143\u683c\u7684\u6574\u6570\u5750\u6807\u4e3a (r, c)\uff0c\u6ee1\u8db3 0 &lt;= r &lt; R \u4e14&#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\/519"}],"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=519"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/519\/revisions"}],"predecessor-version":[{"id":520,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/519\/revisions\/520"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=519"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}