Paint Fill
约 1 分钟· 本文阅读 次
本页目录
Paint Fill
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.
Solution
DFS 遍历一次即可
Complexity
时间复杂度 O(n),空间复杂度 O(n)
Code
// can also be done with BFS
enum Color {Red, Orange, Yellow, Green, Blue, Purple}
private static void PaintFill(Color[][] screen, int r, int c, Color color){
if (screen[r][c] == color)
return;
PaintFill(screen, r, c, screen[r][c], color);
}
private static void PaintFill(Color[][] screen, int r, int c, Color o, Color n){
if (r < 0 || r >= screen.length || c < 0 || c >= screen[0].length){
return;
}
if (screen[r][c] == o){
screen[r][c] = n;
PaintFill(screen, r-1, c, o, n);
PaintFill(screen, r+1, c, o, n);
PaintFill(screen, r, c-1, o, n);
PaintFill(screen, r, c+1, o, n);
}
}相关文章
- All Permutations II2016-01-10
- All Permutation2016-01-10
- All Subsets2016-01-10
- Best Time to Buy and Sell Stock II2016-01-10
已恢复到上次阅读位置