-
-
Save happyWinner/3e03b08e8c394bdfdb4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implement the "paint fill" function that one might see on many image editing programs. | |
* That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color, | |
* fill in the surrounding area until the color changes from the original color. | |
* | |
* Time Complexity: O(mn) | |
* Space Complexity: O(mn) | |
*/ | |
public class Question9_7 { | |
public void paintFill(char[][] screen, int x, int y, char newColor) { | |
if (screen == null || screen.length == 0 || screen[0].length == 0) { | |
return; | |
} | |
int height = screen.length; | |
int width = screen[0].length; | |
if (x < 0 || x >= width || y < 0 || y >= height) { | |
return; | |
} | |
if (screen[y][x] != newColor) { | |
screen[y][x] = newColor; | |
paintFill(screen, x - 1, y, newColor); | |
paintFill(screen, x + 1, y, newColor); | |
paintFill(screen, x, y - 1, newColor); | |
paintFill(screen, x, y + 1, newColor); | |
} | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment