{"id":467,"date":"2020-09-26T10:45:07","date_gmt":"2020-09-26T02:45:07","guid":{"rendered":"http:\/\/39.96.58.60\/?p=467"},"modified":"2022-10-18T16:39:14","modified_gmt":"2022-10-18T08:39:14","slug":"leetcode-463-%e5%b2%9b%e5%b1%bf%e7%9a%84%e5%91%a8%e9%95%bf","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=467","title":{"rendered":"Leetcode 463. \u5c9b\u5c7f\u7684\u5468\u957f"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b 0 \u548c 1 \u7684\u4e8c\u7ef4\u7f51\u683c\u5730\u56fe\uff0c\u5176\u4e2d 1 \u8868\u793a\u9646\u5730&nbsp;0 \u8868\u793a\u6c34\u57df\u3002<\/p>\n<p>\u7f51\u683c\u4e2d\u7684\u683c\u5b50\u6c34\u5e73\u548c\u5782\u76f4\u65b9\u5411\u76f8\u8fde\uff08\u5bf9\u89d2\u7ebf\u65b9\u5411\u4e0d\u76f8\u8fde\uff09\u3002\u6574\u4e2a\u7f51\u683c\u88ab\u6c34\u5b8c\u5168\u5305\u56f4\uff0c\u4f46\u5176\u4e2d\u6070\u597d\u6709\u4e00\u4e2a\u5c9b\u5c7f\uff08\u6216\u8005\u8bf4\uff0c\u4e00\u4e2a\u6216\u591a\u4e2a\u8868\u793a\u9646\u5730\u7684\u683c\u5b50\u76f8\u8fde\u7ec4\u6210\u7684\u5c9b\u5c7f\uff09\u3002<\/p>\n<p>\u5c9b\u5c7f\u4e2d\u6ca1\u6709\u201c\u6e56\u201d\uff08\u201c\u6e56\u201d \u6307\u6c34\u57df\u5728\u5c9b\u5c7f\u5185\u90e8\u4e14\u4e0d\u548c\u5c9b\u5c7f\u5468\u56f4\u7684\u6c34\u76f8\u8fde\uff09\u3002\u683c\u5b50\u662f\u8fb9\u957f\u4e3a 1 \u7684\u6b63\u65b9\u5f62\u3002\u7f51\u683c\u4e3a\u957f\u65b9\u5f62\uff0c\u4e14\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u5747\u4e0d\u8d85\u8fc7 100 \u3002\u8ba1\u7b97\u8fd9\u4e2a\u5c9b\u5c7f\u7684\u5468\u957f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b :<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\n[[0,1,0,0],\n\n [1,1,1,0],\n\n [0,1,0,0],\n\n [1,1,0,0]]\n\n\n\n<strong>\u8f93\u51fa:<\/strong> 16\n\n\n\n<strong>\u89e3\u91ca:<\/strong> \u5b83\u7684\u5468\u957f\u662f\u4e0b\u9762\u56fe\u7247\u4e2d\u7684 16 \u4e2a\u9ec4\u8272\u7684\u8fb9\uff1a\n\n\n\n<img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/10\/12\/island.png\">\n\n<\/pre>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u54c8\u5e0c\u8868\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\uff1a152 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8668.28% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8658.86% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6bcf\u5b58\u5728\u4e00\u4e2a\u76f8\u90bb\u5c9b\u5c7f\uff0c\u5219\u8fb9\u5404\u5c11\u4e00\u6761\u3002\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def islandPerimeter(self, grid: List[List[int]]) -&gt; int:\n        num = 0 # \u8bb0\u5f55\u5c9b\u5c7f\u6570\n        neighbor = 0    # \u8bb0\u5f55\u76f8\u90bb\u5c9b\u5c7f\u6570\n        m, n = len(grid), len(grid[0])  #\n        for i in range(m):\n            for j in range(n):\n                if grid[i][j] == 1: # \u627e\u5230\u5c9b\u5c7f\n                    num += 1    # \u5c9b\u5c7f\u6570+1\n                    if 0 &lt;= i-1 and grid[i-1][j] == 1:  # \u67e5\u770b\u5c9b\u5c7f\u56db\u5468\uff0c\u627e\u76f8\u90bb\u5c9b\u5c7f\n                        neighbor += 1   # \u5b58\u5728\u76f8\u90bb\u5c9b\u5c7f\uff0c\u76f8\u90bb\u5c9b\u5c7f\u6570+1\n                    if i+1 &lt; m and grid[i+1][j] == 1:\n                        neighbor += 1\n                    if 0 &lt;= j-1 and grid[i][j-1] == 1:\n                        neighbor += 1\n                    if j+1 &lt; n and grid[i][j+1] == 1:\n                        neighbor += 1\n        return num*4-neighbor<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b 0 \u548c 1 \u7684\u4e8c\u7ef4\u7f51\u683c\u5730\u56fe\uff0c\u5176\u4e2d 1 \u8868\u793a\u9646\u5730&nbsp;0 \u8868\u793a\u6c34\u57df\u3002 \u7f51\u683c\u4e2d\u7684\u683c\u5b50\u6c34\u5e73\u548c\u5782\u76f4&#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\/467"}],"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=467"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/467\/revisions"}],"predecessor-version":[{"id":468,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/467\/revisions\/468"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}