[Swift]LeetCode463. 島嶼的周長 | Island Perimeter

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-htnhgdah-kr.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.git

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).github

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:ide

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 表示水域。spa

網格中的格子水平和垂直方向相連(對角線方向不相連)。整個網格被水徹底包圍,但其中剛好有一個島嶼(或者說,一個或多個表示陸地的格子相連組成的島嶼)。code

島嶼中沒有「湖」(「湖」 指水域在島嶼內部且不和島嶼周圍的水相連)。格子是邊長爲 1 的正方形。網格爲長方形,且寬度和高度均不超過 100 。計算這個島嶼的周長。 orm

示例 :htm

輸入:
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

輸出: 16

解釋: 它的周長是下面圖片中的 16 個黃色的邊:


244msblog

 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 }
相關文章
相關標籤/搜索