LeetBook--队列&栈--图像渲染
This commit is contained in:
parent
8d00ddc748
commit
dd75655eca
40
src/main/java/leet/book/queueStack/FloodFill.java
Normal file
40
src/main/java/leet/book/queueStack/FloodFill.java
Normal file
@ -0,0 +1,40 @@
|
||||
package leet.book.queueStack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @author: 轩辕龙儿
|
||||
* @date: 2021/7/2 14:14
|
||||
* @Description: No Description
|
||||
*/
|
||||
public class FloodFill {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new FloodFill().new Solution();
|
||||
// int[][] image = solution.floodFill(new int[][]{{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, 1, 1, 2);
|
||||
int[][] image = solution.floodFill(new int[][]{{0, 0, 0}, {0, 1, 1}}, 1, 1, 1);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
|
||||
int oldColor = image[sr][sc];
|
||||
if (oldColor != newColor) {
|
||||
fill(image, sr, sc, newColor, oldColor);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
private void fill(int[][] image, int sr, int sc, int newColor, int oldColor) {
|
||||
if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] != oldColor) {
|
||||
return;
|
||||
}
|
||||
image[sr][sc] = newColor;
|
||||
fill(image, sr - 1, sc, newColor, oldColor);
|
||||
fill(image, sr + 1, sc, newColor, oldColor);
|
||||
fill(image, sr, sc - 1, newColor, oldColor);
|
||||
fill(image, sr, sc + 1, newColor, oldColor);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user