From a605d75ec6d24efb68a03ff7e669de937d6a5891 Mon Sep 17 00:00:00 2001 From: SwaggyP <1352164869@qq.com> Date: Sat, 3 Sep 2022 10:52:35 +0800 Subject: [PATCH] =?UTF-8?q?0463.=E5=B2=9B=E5=B1=BF=E7=9A=84=E5=91=A8?= =?UTF-8?q?=E9=95=BF=E5=A2=9E=E5=8A=A0go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0463.岛屿的周长增加go解法 --- problems/0463.岛屿的周长.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md index 3dc69f20..a881cd7c 100644 --- a/problems/0463.岛屿的周长.md +++ b/problems/0463.岛屿的周长.md @@ -179,6 +179,25 @@ class Solution: ``` Go: +```go +func islandPerimeter(grid [][]int) int { + m, n := len(grid), len(grid[0]) + res := 0 + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + if grid[i][j] == 1 { + res += 4 + // 上下左右四个方向 + if i > 0 && grid[i-1][j] == 1 {res--} // 上边有岛屿 + if i < m-1 && grid[i+1][j] == 1 {res--} // 下边有岛屿 + if j > 0 && grid[i][j-1] == 1 {res--} // 左边有岛屿 + if j < n-1 && grid[i][j+1] == 1 {res--} // 右边有岛屿 + } + } + } + return res +} +``` JavaScript: ```javascript