Skip to content

Commit

Permalink
0200-number-of-islands
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 19, 2018
1 parent 573fcc4 commit d56d58a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 0200-number-of-islands.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// 200. Number of Islands
/// 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.

import XCTest

/// Approach: BFS
func numIslands(_ grid: [[Character]]) -> Int {
var grid = grid
let m = grid.count
guard m > 0 else { return 0}
let n = grid[0].count
var count = 0
for i in 0..<m {
for j in 0..<n {
guard grid[i][j] == "1" else { continue }
count += 1
var queue: [Int] = []
queue.append(i * n + j)
grid[i][j] = "v"
var head = 0
while head < queue.count {
let index = queue[head]
head += 1
let r = index / n
let c = index % n
if r > 0, grid[r - 1][c] == "1" {
queue.append((r - 1) * n + c)
grid[r - 1][c] = "v"
}
if c > 0, grid[r][c - 1] == "1" {
queue.append(r * n + c - 1)
grid[r][c - 1] = "v"
}
if r + 1 < m, grid[r + 1][c] == "1" {
queue.append((r + 1) * n + c)
grid[r + 1][c] = "v"
}
if c + 1 < n, grid[r][c + 1] == "1" {
queue.append(r * n + c + 1)
grid[r][c + 1] = "v"
}
}
}
}
return count
}

class Tests: XCTestCase {
func testExample() {
let grid: [[Character]] = [
["1", "1", "1", "1", "0"],
["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["1", "0", "0", "0", "0"],
]
XCTAssertEqual(numIslands(grid), 1)
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 0200-number-of-islands.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ You can visit the pages below to search problems by company tags. Then come back
33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0033-search-in-rotated-sorted-array.playground/Contents.swift) | Binary Search
66 | [Plus One](https://leetcode.com/problems/plus-one/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0066-plus-one.playground/Contents.swift) | Math
84 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0084-largest-rectangle-in-histogram.playground/Contents.swift) | Stack
200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0200-number-of-islands.playground/Contents.swift) | BFS
482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0482-license-key-formatting.playground/Contents.swift) | String
681 | [Next Closest Time](https://leetcode.com/problems/next-closest-time/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0681-next-closest-time.playground/Contents.swift) | Simulation
683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0683-k-empty-slots.playground/Contents.swift) | Sliding Window
Expand Down

0 comments on commit d56d58a

Please sign in to comment.