Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9.1k
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9084f69

Browse files
authoredMay 8, 2024
feat: add swift implementation to lcci problem: No.16.19 (#2762)
1 parent b26fc05 commit 9084f69

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
 

‎lcci/16.19.Pond Sizes/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,43 @@ function pondSizes(land: number[][]): number[] {
176176
}
177177
```
178178

179+
```swift
180+
class Solution {
181+
private var m: Int = 0
182+
private var n: Int = 0
183+
private var land: [[Int]] = []
184+
185+
func pondSizes(_ land: [[Int]]) -> [Int] {
186+
self.land = land
187+
m = land.count
188+
n = land[0].count
189+
var ans: [Int] = []
190+
191+
for i in 0..<m {
192+
for j in 0..<n {
193+
if self.land[i][j] == 0 {
194+
ans.append(dfs(i, j))
195+
}
196+
}
197+
}
198+
return ans.sorted()
199+
}
200+
201+
private func dfs(_ i: Int, _ j: Int) -> Int {
202+
var res = 1
203+
self.land[i][j] = 1
204+
for x in max(i - 1, 0)...min(i + 1, m - 1) {
205+
for y in max(j - 1, 0)...min(j + 1, n - 1) {
206+
if self.land[x][y] == 0 {
207+
res += dfs(x, y)
208+
}
209+
}
210+
}
211+
return res
212+
}
213+
}
214+
```
215+
179216
<!-- tabs:end -->
180217

181218
<!-- end -->

‎lcci/16.19.Pond Sizes/README_EN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,43 @@ function pondSizes(land: number[][]): number[] {
189189
}
190190
```
191191

192+
```swift
193+
class Solution {
194+
private var m: Int = 0
195+
private var n: Int = 0
196+
private var land: [[Int]] = []
197+
198+
func pondSizes(_ land: [[Int]]) -> [Int] {
199+
self.land = land
200+
m = land.count
201+
n = land[0].count
202+
var ans: [Int] = []
203+
204+
for i in 0..<m {
205+
for j in 0..<n {
206+
if self.land[i][j] == 0 {
207+
ans.append(dfs(i, j))
208+
}
209+
}
210+
}
211+
return ans.sorted()
212+
}
213+
214+
private func dfs(_ i: Int, _ j: Int) -> Int {
215+
var res = 1
216+
self.land[i][j] = 1
217+
for x in max(i - 1, 0)...min(i + 1, m - 1) {
218+
for y in max(j - 1, 0)...min(j + 1, n - 1) {
219+
if self.land[x][y] == 0 {
220+
res += dfs(x, y)
221+
}
222+
}
223+
}
224+
return res
225+
}
226+
}
227+
```
228+
192229
<!-- tabs:end -->
193230

194231
<!-- end -->

‎lcci/16.19.Pond Sizes/Solution.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
private var m: Int = 0
3+
private var n: Int = 0
4+
private var land: [[Int]] = []
5+
6+
func pondSizes(_ land: [[Int]]) -> [Int] {
7+
self.land = land
8+
m = land.count
9+
n = land[0].count
10+
var ans: [Int] = []
11+
12+
for i in 0..<m {
13+
for j in 0..<n {
14+
if self.land[i][j] == 0 {
15+
ans.append(dfs(i, j))
16+
}
17+
}
18+
}
19+
return ans.sorted()
20+
}
21+
22+
private func dfs(_ i: Int, _ j: Int) -> Int {
23+
var res = 1
24+
self.land[i][j] = 1
25+
for x in max(i - 1, 0)...min(i + 1, m - 1) {
26+
for y in max(j - 1, 0)...min(j + 1, n - 1) {
27+
if self.land[x][y] == 0 {
28+
res += dfs(x, y)
29+
}
30+
}
31+
}
32+
return res
33+
}
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.