You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn‘t have "lakes" (water inside that isn‘t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don‘t exceed 100. Determine the perimeter of the island.
Example:
Input: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Output: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。
网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。
岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。
示例 :
输入: [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] 输出: 16 解释: 它的周长是下面图片中的 16 个黄色的边:
244ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 var landCount = 0 4 var neighborCount = 0 5 6 for i in 0..<grid.count { 7 for j in 0..<grid[i].count { 8 if grid[i][j] == 1 { 9 landCount += 1 10 if i < grid.count - 1 && grid[i + 1][j] == 1 { 11 neighborCount += 1 12 } 13 if j < grid[i].count - 1 && grid[i][j + 1] == 1 { 14 neighborCount += 1 15 } 16 } 17 } 18 } 19 20 return landCount * 4 - neighborCount * 2 21 } 22 }
276ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 var count:Int = 0 4 for i in 0..<grid.count { 5 for j in 0..<grid[i].count{ 6 if(grid[i][j] == 1){ 7 count += 4; 8 } 9 if(grid[i][j] == 1 && i - 1 >= 0 && grid[i - 1][j] == 1){ 10 count -= 2; 11 } 12 if(grid[i][j] == 1 && j - 1 >= 0 && grid[i][j - 1] == 1){ 13 count -= 2; 14 } 15 } 16 } 17 return count 18 } 19 }
504ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 var perimeter: Int = 0 4 for i in 0..<grid.count { 5 for j in 0..<grid[i].count { 6 if grid[i][j] == 1 { 7 var commonEdges = 0 8 9 if i > 0 && grid[i - 1][j] == 1 { 10 commonEdges += 1 11 } 12 if i < grid.count - 1 && grid[i + 1][j] == 1 { 13 commonEdges += 1 14 } 15 16 if j > 0 && grid[i][j - 1] == 1 { 17 commonEdges += 1 18 } 19 if j < grid[i].count - 1 && grid[i][j + 1] == 1 { 20 commonEdges += 1 21 } 22 23 perimeter += 4 - commonEdges 24 } 25 } 26 } 27 return perimeter 28 } 29 }
528ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 var perimeterTotal = 0 4 if grid.count == 0 { 5 return 0 6 } 7 perimeterTotal = lrBoundTotal(grid) + tbBoundTotal(grid) 8 return perimeterTotal 9 } 10 11 func lrBoundTotal(_ grid:[[Int]]) -> Int { 12 let height = grid.count 13 let width = grid[0].count 14 var blockCount = 0 15 for i in 0..<height { 16 var emptyWater = 1 17 for j in 0..<width { 18 let val = grid[i][j] 19 if (val == 1) { 20 if (emptyWater == 1) { 21 blockCount += 1 22 } 23 emptyWater = 0 24 } else { 25 emptyWater = 1 26 } 27 } 28 } 29 return blockCount * 2 30 } 31 32 func tbBoundTotal(_ grid:[[Int]]) -> Int { 33 let height = grid.count 34 let width = grid[0].count 35 var blockCount = 0 36 for j in 0..<width { 37 var emptyWater = 1 38 for i in 0..<height { 39 let val = grid[i][j] 40 if (val == 1) { 41 if (emptyWater == 1) { 42 blockCount += 1 43 } 44 emptyWater = 0 45 } else { 46 emptyWater = 1 47 } 48 } 49 } 50 return blockCount * 2 51 } 52 }
576ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 var matrix = grid 4 var perim = 0 5 for i in 0..<grid.count{ 6 for j in 0..<grid[0].count{ 7 if grid[i][j] == 1{ 8 perim += perime(&matrix, (i, j)) 9 } 10 } 11 } 12 return perim 13 } 14 15 func perime(_ grid: inout [[Int]], _ point: (Int, Int))->Int{ 16 var count = 0 17 if point.0 == 0{ 18 count += 1 19 }else{ 20 if grid[point.0 - 1][point.1] == 0{ 21 count += 1 22 } 23 } 24 25 if point.0 == grid.count - 1{ 26 count += 1 27 }else{ 28 if grid[point.0 + 1][point.1] == 0{ 29 count += 1 30 } 31 } 32 33 if point.1 == 0{ 34 count += 1 35 }else{ 36 if grid[point.0][point.1 - 1] == 0{ 37 count += 1 38 } 39 } 40 41 if point.1 == grid[0].count - 1{ 42 count += 1 43 }else{ 44 if grid[point.0][point.1 + 1] == 0{ 45 count += 1 46 } 47 } 48 return count 49 } 50 }
624ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 let rows = grid.count 4 let cols = grid[0].count 5 var sum = 0 6 for i in 0...rows-1 7 { 8 for j in 0...cols-1 9 { 10 if grid[i][j] == 1 11 { 12 if (i-1 >= 0 && grid[i-1][j] == 0) || i-1 < 0 { 13 sum += 1 14 } 15 if (i+1 < rows && grid[i+1][j] == 0) || i+1 == rows{ 16 sum += 1 17 } 18 if (j-1 >= 0 && grid [i][j-1] == 0) || j-1 < 0{ 19 sum += 1 20 } 21 if (j+1 < cols && grid[i][j+1] == 0) || j+1 == cols{ 22 sum += 1 23 } 24 } 25 } 26 } 27 return sum 28 } 29 }
648ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 if grid.isEmpty || grid[0].isEmpty {return 0} 4 var res:Int = 0 5 var m:Int = grid.count 6 var n:Int = grid[0].count 7 for i in 0..<m 8 { 9 for j in 0..<n 10 { 11 if grid[i][j] == 0 12 { 13 continue 14 } 15 res += 4 16 if i > 0 && grid[i - 1][j] == 1 17 { 18 res -= 2 19 } 20 if j > 0 && grid[i][j - 1] == 1 21 { 22 res -= 2 23 } 24 } 25 } 26 return res 27 } 28 }
964ms
1 class Solution { 2 func islandPerimeter(_ grid: [[Int]]) -> Int { 3 var sum = 0 4 for i in 0..<grid.count { 5 let g = grid[i] 6 for x in 0..<g.count{ 7 let top = i-1 < 0 ? 0 : grid[i-1][x] 8 let left = x-1 < 0 ? 0 :g[x-1] 9 let bottom = i+1 >= grid.count ? 0 : grid[i+1][x] 10 let right = x+1 >= g.count ? 0 :g[x+1] 11 let z = g[x] 12 13 if z == 1{ 14 sum = sum + 4 - (top + left + bottom + right) 15 } 16 } 17 } 18 return sum 19 } 20 }
原文地址:https://www.cnblogs.com/strengthen/p/10345792.html