diff --git a/lcci/16.19.Pond Sizes/README.md b/lcci/16.19.Pond Sizes/README.md index 0da952d32f000..3c433230f45bc 100644 --- a/lcci/16.19.Pond Sizes/README.md +++ b/lcci/16.19.Pond Sizes/README.md @@ -176,6 +176,43 @@ function pondSizes(land: number[][]): number[] { } ``` +```swift +class Solution { + private var m: Int = 0 + private var n: Int = 0 + private var land: [[Int]] = [] + + func pondSizes(_ land: [[Int]]) -> [Int] { + self.land = land + m = land.count + n = land[0].count + var ans: [Int] = [] + + for i in 0.. Int { + var res = 1 + self.land[i][j] = 1 + for x in max(i - 1, 0)...min(i + 1, m - 1) { + for y in max(j - 1, 0)...min(j + 1, n - 1) { + if self.land[x][y] == 0 { + res += dfs(x, y) + } + } + } + return res + } +} +``` + diff --git a/lcci/16.19.Pond Sizes/README_EN.md b/lcci/16.19.Pond Sizes/README_EN.md index f58c8d83e28db..f56e18cba6510 100644 --- a/lcci/16.19.Pond Sizes/README_EN.md +++ b/lcci/16.19.Pond Sizes/README_EN.md @@ -189,6 +189,43 @@ function pondSizes(land: number[][]): number[] { } ``` +```swift +class Solution { + private var m: Int = 0 + private var n: Int = 0 + private var land: [[Int]] = [] + + func pondSizes(_ land: [[Int]]) -> [Int] { + self.land = land + m = land.count + n = land[0].count + var ans: [Int] = [] + + for i in 0.. Int { + var res = 1 + self.land[i][j] = 1 + for x in max(i - 1, 0)...min(i + 1, m - 1) { + for y in max(j - 1, 0)...min(j + 1, n - 1) { + if self.land[x][y] == 0 { + res += dfs(x, y) + } + } + } + return res + } +} +``` + diff --git a/lcci/16.19.Pond Sizes/Solution.swift b/lcci/16.19.Pond Sizes/Solution.swift new file mode 100644 index 0000000000000..f3321f4961bc0 --- /dev/null +++ b/lcci/16.19.Pond Sizes/Solution.swift @@ -0,0 +1,34 @@ +class Solution { + private var m: Int = 0 + private var n: Int = 0 + private var land: [[Int]] = [] + + func pondSizes(_ land: [[Int]]) -> [Int] { + self.land = land + m = land.count + n = land[0].count + var ans: [Int] = [] + + for i in 0.. Int { + var res = 1 + self.land[i][j] = 1 + for x in max(i - 1, 0)...min(i + 1, m - 1) { + for y in max(j - 1, 0)...min(j + 1, n - 1) { + if self.land[x][y] == 0 { + res += dfs(x, y) + } + } + } + return res + } +} \ No newline at end of file