日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[Swift]LeetCode463. 岛屿的周长 | Island Perimeter

發(fā)布時(shí)間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Swift]LeetCode463. 岛屿的周长 | Island Perimeter 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/10345792.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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: 16Explanation: The perimeter is the 16 yellow stripes in the image below:


給定一個(gè)包含 0 和 1 的二維網(wǎng)格地圖,其中 1 表示陸地?0 表示水域。

網(wǎng)格中的格子水平和垂直方向相連(對(duì)角線方向不相連)。整個(gè)網(wǎng)格被水完全包圍,但其中恰好有一個(gè)島嶼(或者說(shuō),一個(gè)或多個(gè)表示陸地的格子相連組成的島嶼)。

島嶼中沒有“湖”(“湖” 指水域在島嶼內(nèi)部且不和島嶼周圍的水相連)。格子是邊長(zhǎng)為 1 的正方形。網(wǎng)格為長(zhǎng)方形,且寬度和高度均不超過 100 。計(jì)算這個(gè)島嶼的周長(zhǎng)。?

示例 :

輸入: [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]輸出: 16解釋: 它的周長(zhǎng)是下面圖片中的 16 個(gè)黃色的邊:


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 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/10345792.html

總結(jié)

以上是生活随笔為你收集整理的[Swift]LeetCode463. 岛屿的周长 | Island Perimeter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。