为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode695. 岛屿的最大面积 | Max Area of Island

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10502593.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

Note: The length of each dimension in the given grid does not exceed 50.


给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。

找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)

示例 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]

对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。

示例 2:

[[0,0,0,0,0,0,0,0]]

对于上面这个给定的矩阵, 返回 0

注意: 给定的矩阵grid 的长度和宽度都不超过 50。


Runtime: 76 ms
Memory Usage: 20 MB
 1 class Solution {
 2     func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
 3         var tempGrid = grid
 4         var area = 0
 5         let m = tempGrid.count
 6         var n = 0
 7         var islandMax = 0
 8         for i in 0..<m {
 9             n = tempGrid[i].count
10             for j in 0..<n {
11                 area = 0
12                 area = dfs(i, j, m, n, &tempGrid, &area)
13                 islandMax = max(islandMax, area)
14             }
15         }
16         return islandMax    
17     }
18     
19     func dfs(_ i: Int,_ j: Int,_ m: Int,_ n:Int,_ grid: inout [[Int]],_ tempArea: inout Int) -> Int {
20         if i<0 || i>=m || j<0 || j>=n || grid[i][j] == 0 {
21             return tempArea
22         }
23         grid[i][j] = 0
24         tempArea += 1
25         tempArea = dfs(i+1, j, m, n, &grid, &tempArea)
26         tempArea = dfs(i-1, j, m, n, &grid, &tempArea)
27         tempArea = dfs(i, j+1, m, n, &grid, &tempArea)
28         tempArea = dfs(i, j-1, m, n, &grid, &tempArea)
29         return tempArea
30     }
31 }

80ms

 1 class Solution {
 2     func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
 3         var dp:[[Int]] = grid
 4         var mX: Int = 0
 5         
 6         for i in 0 ..< dp.count {
 7             for j in 0 ..< dp[0].count {
 8                 // print("i:\(i) j:\(j)")
 9                 if dp[i][j] == 1 {
10                     mX = max(mX, area(&dp,i,j))
11                 }
12             }
13         }
14         
15         return mX
16     }
17     
18     func area(_ dp: inout [[Int]], _ i:Int, _ j:Int) -> Int {
19         if i < dp.count && i >= 0 && j < dp[0].count && j >= 0 && dp[i][j] == 1 {
20             dp[i][j] = 0
21             return 1 + area(&dp, i-1, j) + area(&dp, i+1, j) + area(&dp, i, j-1) + area(&dp, i, j+1)
22         }
23         return 0
24     }
25 }

92ms

 1 class Solution {
 2     var height = 0
 3     var width = 0
 4     
 5     var grid: [[Int]]!
 6     var visited: [[Bool]]!
 7     
 8     func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
 9         height = grid.count
10         width = grid[0].count
11         
12         self.grid = grid
13         
14         var maxSize = 0
15         visited = Array(repeating:Array(repeating: false, count: width), count: height)
16         
17         for y in 0..<grid.count {
18             for x in 0..<grid[0].count {
19                 if grid[y][x] == 1 && visited[y][x] == false {
20                     let size = visitIsland(x: x, y: y)
21                     if size > maxSize {
22                         maxSize = size
23                     }
24                 }
25             }
26         }
27         return maxSize
28     }
29     
30     
31     func visitIsland(x: Int, y: Int) -> Int {
32      //   print("visit island x: \(x) y: \(y)")
33         if x < 0 || y < 0 || x >= width || y >= height {
34             return 0
35         }
36         if grid[y][x] == 0 || visited[y][x] == true {
37             return 0
38         }
39         visited[y][x] = true
40         var size = 1
41         size = size + visitIsland(x: x-1, y: y)
42         size = size + visitIsland(x: x+1, y: y)
43         size = size + visitIsland(x: x, y: y-1)
44         size = size + visitIsland(x: x, y: y+1)
45         return size
46     }
47 }

108ms

 1 class Solution {
 2     var maxCount = 0
 3     let transform = [[0,1],[0,-1],[1,0],[-1,0]]
 4     func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
 5         var grid = grid
 6         for i in 0..<grid.count {
 7             for j in 0..<grid[0].count {
 8                 if grid[i][j] == 1 {
 9                     let count = dfs(&grid, i, j)
10                     if maxCount < count {
11                         maxCount = count 
12                     }
13                 }
14             }
15         }
16         return maxCount
17     }
18     
19     func dfs(_ grid: inout [[Int]], _ x: Int, _ y: Int) -> Int {
20         if x >= 0 && x < grid.count && y >= 0 && y < grid[0].count && grid[x][y] == 1 {
21             grid[x][y] = 0
22             var count = 1
23             for i in 0..<4 {
24                 let xx = x + transform[i][0]
25                 let yy = y + transform[i][1]
26                 count = count + dfs(&grid, xx, yy)
27             }   
28             return count
29         } else {
30             return 0
31         }
32     }
33 }

108ms

 1 class Solution {
 2     func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
 3   var maxArea = 0
 4   var grid = grid
 5   for i in 0..<grid.count {
 6     for j in 0..<grid[i].count {
 7       if grid[i][j] == 1 {
 8         maxArea = max(maxArea, area(&grid, i, j))
 9       }
10     }
11   }
12   return maxArea
13 }
14 
15 func area(_ grid:inout[[Int]], _ i: Int, _ j: Int) -> Int {
16   var size = 0
17   if(i>=0 && i<grid.count && j>=0 && j<grid[0].count && grid[i][j]==1) {
18     grid[i][j] = 0
19     size = 1 + 
20     area(&grid, i+1, j) + 
21     area(&grid, i-1, j) + 
22     area(&grid, i, j-1) + 
23     area(&grid, i, j+1)
24   }
25   return size
26 }
27 }

112ms

 1 class Solution {
 2     func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
 3         if grid.isEmpty || grid[0].isEmpty { return 0 }
 4         var res = 0
 5         var grid = grid
 6         let m = grid.count, n = grid[0].count
 7         for i in 0..<m {
 8             for j in 0..<n {
 9                 if grid[i][j] == 0 { continue }
10                 var count = 0
11                 var worklist = [(i, j)]
12                 while !worklist.isEmpty {
13                     let (x, y) = worklist.popLast()!
14                     if grid[x][y] == 0 { continue }
15                     grid[x][y] = 0
16                     count += 1
17                     if x > 0  { worklist.append((x - 1, y)) }
18                     if y > 0 { worklist.append((x, y - 1)) }
19                     if x < m - 1 { worklist.append((x + 1, y)) }
20                     if y < n - 1 { worklist.append((x, y + 1)) }
21                 }
22                 res = max(res, count)
23             }
24         }
25         return res
26     }
27 }

132 ms

 1 class Solution {
 2     var dirs:[[Int]] = [[0,-1],[-1,0],[0,1],[1,0]]
 3     func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
 4         var grid = grid
 5         var m:Int = grid.count
 6         var n:Int = grid[0].count
 7         var res:Int = 0
 8         for i in 0..<m
 9         {
10             for j in 0..<n
11             {
12                 if grid[i][j] != 1
13                 {
14                     continue
15                 }
16                 var cnt:Int = 0
17                 var q:[(Int,Int)] = [(i,j)]
18                 grid[i][j] *= -1
19                 while (!q.isEmpty)
20                 {
21                     var t:(Int,Int) = q.first!
22                     q.removeFirst()
23                     cnt += 1
24                     res = max(res, cnt)                    
25                     for dir in dirs
26                     {
27                         var x:Int = t.0 + dir[0]
28                         var y:Int = t.1 + dir[1]
29                         if x < 0 || x >= m || y < 0 || y >= n || grid[x][y] <= 0
30                         {
31                             continue
32                         }
33                         grid[x][y] *= -1
34                         q.append((x, y))
35                     }
36                 }
37             }
38         }
39         return res
40     }
41 }

 

posted @ 2019-03-09 19:14  为敢技术  阅读(407)  评论(0编辑  收藏  举报