File tree Expand file tree Collapse file tree 3 files changed +108
-0
lines changed Expand file tree Collapse file tree 3 files changed +108
-0
lines changed Original file line number Diff line number Diff line change @@ -176,6 +176,43 @@ function pondSizes(land: number[][]): number[] {
176
176
}
177
177
```
178
178
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
+
179
216
<!-- tabs: end -->
180
217
181
218
<!-- end -->
Original file line number Diff line number Diff line change @@ -189,6 +189,43 @@ function pondSizes(land: number[][]): number[] {
189
189
}
190
190
```
191
191
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
+
192
229
<!-- tabs: end -->
193
230
194
231
<!-- end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments