733. 图像渲染_简单_矩阵

 

 

class Solution {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
            int [][]foot = new int[image.length][image[0].length];
            deleteIslands(sr,sc,newColor,image[sr][sc],image,foot);
            return image;
    }

        int deleteIslands(int i,int j,int newColor,int oldColor,int [][]grid,int [][]foot){
        foot[i][j] =1;
        if(grid[i][j] != oldColor)  return 0;
        else{
            grid[i][j] = newColor;
            if(i<grid.length-1 && foot[i+1][j]==0 && grid[i+1][j]==oldColor){
                deleteIslands(i+1,j, newColor,oldColor,grid,foot);
            }
            if(j<grid[0].length-1 && foot[i][j+1]==0 && grid[i][j+1]==oldColor){
                deleteIslands(i,j+1, newColor,oldColor,grid,foot);
            }
            if(i>0 && foot[i-1][j]==0 && grid[i-1][j]==oldColor){
                deleteIslands(i-1,j,newColor,oldColor, grid,foot);
            }
            if(j>0 && foot[i][j-1]==0 && grid[i][j-1]==oldColor){
                deleteIslands(i,j-1,newColor,oldColor, grid,foot);
            }
            return 0;
        }
        
    }
}

 

posted @ 2021-09-29 23:15  你的雷哥  阅读(95)  评论(0编辑  收藏  举报