Skip to content

Instantly share code, notes, and snippets.

@happyWinner
Created August 8, 2014 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save happyWinner/3e03b08e8c394bdfdb4d to your computer and use it in GitHub Desktop.
Save happyWinner/3e03b08e8c394bdfdb4d to your computer and use it in GitHub Desktop.
/**
* 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