博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode200.岛屿的个数 | Number of Islands
阅读量:5119 次
发布时间:2019-06-13

本文共 6476 字,大约阅读时间需要 21 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input:11110110101100000000Output: 1

Example 2:

Input:11000110000010000011Output: 3

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input:11110110101100000000Output: 1

Example 2:

Input:11000110000010000011Output: 3

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例 1:

输入:11110110101100000000输出: 1

示例 2:

输入:11000110000010000011输出: 3

260ms
1 class Solution { 2     func numIslands(_ grid: [[Character]]) -> Int { 3         var map = grid 4         let row = map.count 5         if row == 0 {
return 0} 6 let col = map[0].count 7 var count = 0 8 for i in 0..
= 0 && i < row && j >= 0 && j < col && map[i][j] == "1") {20 map[i][j] = "2"21 dfs(&map, i, j-1, row, col);22 dfs(&map, i-1, j, row, col);23 dfs(&map, i, j+1, row, col);24 dfs(&map, i+1, j, row, col);25 }26 }27 }

268ms

1 class Solution { 2     func numIslands(_ grid: [[Character]]) -> Int { 3         var map = grid 4         let row = map.count 5         if row == 0 {
return 0} 6 let col = map[0].count 7 var count = 0 8 for i in 0..
= 0 && i < row && j >= 0 && j < col && map[i][j] == "1") {20 map[i][j] = "2"21 let index = [0,-1,0,1,0]22 for k in 0..<(index.count - 1) {23 dfs(&map, i + index[k], j + index[k + 1], row, col)24 }25 }26 }27 }

288ms

1 class Solution { 2   func numIslands(_ grid: [[Character]]) -> Int { 3         var grid = grid 4         var count = 0 5          6         for row in grid.indices { 7             for col in 0..
0, grid[x - 1][y] == "1" {34 grid[x - 1][y] = "0" // mark as visited35 queue.append((x - 1, y))36 }37 38 //down part39 if x < grid.count - 1, grid[x + 1][y] == "1" {40 grid[x + 1][y] = "0" // mark as visited41 queue.append((x + 1, y))42 }43 44 //left part45 if y > 0, grid[x][y - 1] == "1" {46 grid[x ][y - 1] = "0" // mark as visited47 queue.append((x, y - 1))48 }49 50 //right part51 if y < grid[x].count - 1, grid[x][y + 1] == "1" {52 grid[x ][y + 1] = "0" // mark as visited53 queue.append((x, y + 1))54 }55 }56 }57 58 func dfs(grid: inout [[Character]], row: Int, col: Int) {59 //dfs: pre order tree: me left righ => me, up, down, left right60 61 // me step62 guard grid[row][col] == "1" else { return }63 64 grid[row][col] = "0"// mark as visited, smart65 66 //up part67 if row > 0, grid[row - 1][col] == "1" {68 dfs(grid: &grid, row: row - 1, col: col)69 }70 71 //down part72 if row < grid.count - 1, grid[row + 1][col] == "1" {73 dfs(grid: &grid, row: row + 1, col: col)74 }75 76 //left part77 if col > 0, grid[row][col - 1] == "1" {78 dfs(grid: &grid, row: row, col: col - 1)79 }80 81 //right part82 if col < grid[row].count - 1, grid[row][col + 1] == "1" {83 dfs(grid: &grid, row: row, col: col + 1)84 }85 86 }87 }

292ms

1 class Solution { 2     func numIslands(_ grid: [[Character]]) -> Int { 3         guard !grid.isEmpty, !grid[0].isEmpty else { return 0 } 4          5         var queue = [[Int]]() 6         var grid = grid 7         var count = 0 8          9         for i in 0..
= grid.count || column < 0 || column >= grid[0].count || grid[row][column] == "0" {32 continue33 }34 grid[row][column] = "0"35 queue.append([row, column])36 }37 }38 }39 }

296ms

1 class Solution { 2     func numIslands(_ grid: [[Character]]) -> Int { 3         guard grid.count > 0, grid[0].count > 0 else { 4             return 0 5         } 6         var result = 0 7          8         var dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]] 9         10         var memo = [[Bool]](repeating:[Bool](repeating:false, count:grid[0].count), count:grid.count)11         12         func dfs(i:Int, j:Int) {13             if i < 0 || j < 0 || i >= grid.count || j >= grid[0].count || memo[i][j] == true || grid[i][j] == "0" {14                 return15             }16             17             memo[i][j] = true18             19             for dir in dirs {20                 dfs(i:i + dir[0], j: j + dir[1])21             }22         }23         24         for i in 0..

452ms

1 class Solution { 2      3     func numIslands(_ grid: [[Character]]) -> Int { 4          5         var map = grid 6         let n = map.count; 7         if n == 0 { return 0 } 8         let m = map[0].count 9         var count = 010         11          func emptMap(_ map: inout [[Character]], i: Int, j: Int) {12             if (i < 0 || j<0 || i>=n || j>=m || map[i][j] != "1") { return }13             map[i][j] = "0";14             // 递归清除所有地点15             emptMap(&map, i: i+1, j: j)16             emptMap(&map, i: i-1, j: j)17             emptMap(&map, i: i, j: j+1)18             emptMap(&map, i: i, j: j-1)19         }20         21         for (i,v) in map.enumerated() {22             for (j,_) in v.enumerated () {23                 if (map[i][j] == "1") {24                     emptMap(&map, i: i, j: j)25                     count = count + 126                 }27             }28         }29         30         return count31     }32 }

 

转载于:https://www.cnblogs.com/strengthen/p/10180994.html

你可能感兴趣的文章
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
Dirichlet分布深入理解
查看>>
字符串处理
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>
获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName...
查看>>
证件照(1寸2寸)拍摄处理知识汇总
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>
一道不知道哪里来的容斥题
查看>>
window添加右键菜单
查看>>
Window7上搭建symfony开发环境(PEAR)
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
一些方便系统诊断的bash函数
查看>>
jquery中ajax返回值无法传递到上层函数
查看>>