-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAprilSolDay17.swift
46 lines (38 loc) · 1.21 KB
/
AprilSolDay17.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// AprilSolDay17.swift
// LeetCodeChallenge
//
// Created by Michael Ho on 4/21/20.
// Copyright © 2020 Michael Ho. All rights reserved.
//
// LeetCode: https://leetcode.com/problems/number-of-islands/
class AprilSolDay17 {
func numIslands(_ grid: [[Character]]) -> Int {
var count = 0
var matrix = grid
for x in 0..<matrix.count {
for y in 0..<matrix[x].count {
if matrix[x][y] == "1" {
count += 1
findAdjacentIslands(x, y, &matrix)
}
}
}
return count
}
func findAdjacentIslands(_ x: Int, _ y: Int, _ matrix: inout [[Character]]) {
matrix[x][y] = "0"
if x + 1 < matrix.count, matrix[x + 1][y] == "1" {
findAdjacentIslands(x + 1, y, &matrix)
}
if y + 1 < matrix[x].count, matrix[x][y + 1] == "1" {
findAdjacentIslands(x, y + 1, &matrix)
}
if y - 1 >= 0, matrix[x][y - 1] == "1" {
findAdjacentIslands(x, y - 1, &matrix)
}
if x - 1 >= 0, matrix[x - 1][y] == "1" {
findAdjacentIslands(x - 1, y, &matrix)
}
}
}