Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d6c96a0

Browse filesBrowse files
committed
Flood Fill C# solution
1 parent 146d180 commit d6c96a0
Copy full SHA for d6c96a0

File tree

1 file changed

+32
-0
lines changed
Filter options

1 file changed

+32
-0
lines changed
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Runtime: 248 ms, faster than 93.23% of C# online submissions for Flood Fill.
2+
// Memory Usage: 32.7 MB, less than 73.36% of C# online submissions for Flood Fill.
3+
public class Solution
4+
{
5+
public int[][] FloodFill(int[][] image, int sr, int sc, int newColor)
6+
{
7+
//sr is starting row position, sc is column position
8+
if (image[sr][sc] == newColor)
9+
{
10+
return image;
11+
}
12+
13+
DfsFill(image, sr, sc, image[sr][sc], newColor);
14+
15+
return image;
16+
}
17+
18+
public void DfsFill(int[][] image, int row, int column, int oldColor, int newColor)
19+
{
20+
if (row < 0 || row >= image.Length || column < 0 || column >= image[row].Length || image[row][column] != oldColor)
21+
{
22+
return;
23+
}
24+
25+
image[row][column] = newColor;
26+
27+
DfsFill(image, row - 1, column, oldColor, newColor);
28+
DfsFill(image, row + 1, column, oldColor, newColor);
29+
DfsFill(image, row, column - 1, oldColor, newColor);
30+
DfsFill(image, row, column + 1, oldColor, newColor);
31+
}
32+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.