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;
        }
        
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章