Skip to content

Commit

Permalink
Merge pull request #51 from radiantchoi/main
Browse files Browse the repository at this point in the history
241210 - LeetCode No.733 Flood Fill
  • Loading branch information
bbbjihan authored Dec 12, 2024
2 parents cdc2395 + 61fb694 commit 48151a8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Kyungmin_Choi/Swift/FloodFill.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// LeetCode No.733 Flood Fill
/// https://leetcode.com/problems/flood-fill/description/

class Solution {
func floodFill(_ image: [[Int]], _ sr: Int, _ sc: Int, _ color: Int) -> [[Int]] {
var image = image
let previous = image[sr][sc]
if color == previous {
return image
}

traverse(&image, sr, sc, previous, color)

return image
}

func traverse(_ image: inout [[Int]], _ row: Int, _ col: Int, _ previous: Int, _ color: Int) {
guard (0..<image.count) ~= row && (0..<image[0].count) ~= col else { return }
guard image[row][col] == previous else { return }

image[row][col] = color

traverse(&image, row+1, col, previous, color)
traverse(&image, row-1, col, previous, color)
traverse(&image, row, col+1, previous, color)
traverse(&image, row, col-1, previous, color)
}
}

0 comments on commit 48151a8

Please sign in to comment.