classSolution { constexprstaticint dx[4] = {0, 1, 0, -1}; constexprstaticint dy[4] = {1, 0, -1, 0}; public: intislandPerimeter(vector<vector<int>> &grid){ int n = grid.size(), m = grid[0].size(); int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j]) { int cnt = 0; for (int k = 0; k < 4; ++k) { int tx = i + dx[k]; int ty = j + dy[k]; if (tx < 0 || tx >= n || ty < 0 || ty >= m || !grid[tx][ty]) { cnt += 1; } } ans += cnt; } } } return ans; } };
var islandPerimeter = function (grid) { const dx = [0, 1, 0, -1]; const dy = [1, 0, -1, 0]; const n = grid.length, m = grid[0].length; let ans = 0; for (let i = 0; i < n; ++i) { for (let j = 0; j < m; ++j) { if (grid[i][j]) { let cnt = 0; for (let k = 0; k < 4; ++k) { let tx = i + dx[k]; let ty = j + dy[k]; if (tx < 0 || tx >= n || ty < 0 || ty >= m || !grid[tx][ty]) { cnt += 1; } } ans += cnt; } } } return ans; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
type pair struct{ x, y int } var dir4 = []pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
funcislandPerimeter(grid [][]int) (ans int) { n, m := len(grid), len(grid[0]) for i, row := range grid { for j, v := range row { if v == 1 { for _, d := range dir4 { if x, y := i+d.x, j+d.y; x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == 0 { ans++ } } } } } return }
intislandPerimeter(int** grid, int gridSize, int* gridColSize) { int n = gridSize, m = gridColSize[0]; int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j]) { int cnt = 0; for (int k = 0; k < 4; ++k) { int tx = i + dx[k]; int ty = j + dy[k]; if (tx < 0 || tx >= n || ty < 0 || ty >= m || !grid[tx][ty]) { cnt += 1; } } ans += cnt; } } } return ans; }
classSolution { constexprstaticint dx[4] = {0, 1, 0, -1}; constexprstaticint dy[4] = {1, 0, -1, 0}; public: intdfs(int x, int y, vector<vector<int>> &grid, int n, int m){ if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == 0) { return1; } if (grid[x][y] == 2) { return0; } grid[x][y] = 2; int res = 0; for (int i = 0; i < 4; ++i) { int tx = x + dx[i]; int ty = y + dy[i]; res += dfs(tx, ty, grid, n, m); } return res; } intislandPerimeter(vector<vector<int>> &grid){ int n = grid.size(), m = grid[0].size(); int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] == 1) { ans += dfs(i, j, grid, n, m); } } } return ans; } };
publicintislandPerimeter(int[][] grid) { intn= grid.length, m = grid[0].length; intans=0; for (inti=0; i < n; ++i) { for (intj=0; j < m; ++j) { if (grid[i][j] == 1) { ans += dfs(i, j, grid, n, m); } } } return ans; }
publicintdfs(int x, int y, int[][] grid, int n, int m) { if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == 0) { return1; } if (grid[x][y] == 2) { return0; } grid[x][y] = 2; intres=0; for (inti=0; i < 4; ++i) { inttx= x + dx[i]; intty= y + dy[i]; res += dfs(tx, ty, grid, n, m); } return res; } }
var islandPerimeter = function (grid) { const dx = [0, 1, 0, -1]; const dy = [1, 0, -1, 0]; const n = grid.length, m = grid[0].length;
constdfs = (x, y) => { if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] === 0) { return1; } if (grid[x][y] === 2) { return0; } grid[x][y] = 2; let res = 0; for (let i = 0; i < 4; ++i) { const tx = x + dx[i]; const ty = y + dy[i]; res += dfs(tx, ty); } return res; }
let ans = 0; for (let i = 0; i < n; ++i) { for (let j = 0; j < m; ++j) { if (grid[i][j] === 1) { ans += dfs(i, j); } } } return ans; };
type pair struct{ x, y int } var dir4 = []pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
funcislandPerimeter(grid [][]int) (ans int) { n, m := len(grid), len(grid[0]) var dfs func(x, y int) dfs = func(x, y int) { if x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == 0 { ans++ return } if grid[x][y] == 2 { return } grid[x][y] = 2 for _, d := range dir4 { dfs(x+d.x, y+d.y) } } for i, row := range grid { for j, v := range row { if v == 1 { dfs(i, j) } } } return }
intdfs(int x, int y, int** grid, int n, int m) { if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == 0) { return1; } if (grid[x][y] == 2) { return0; } grid[x][y] = 2; int res = 0; for (int i = 0; i < 4; ++i) { int tx = x + dx[i]; int ty = y + dy[i]; res += dfs(tx, ty, grid, n, m); } return res; }
intislandPerimeter(int** grid, int gridSize, int* gridColSize) { int n = gridSize, m = gridColSize[0]; int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] == 1) { ans += dfs(i, j, grid, n, m); } } } return ans; }