minjs1cn / weekly-learning

每周学习分享打卡
0 stars 0 forks source link

46 -【leetcode】颜色填充 #46

Open wordlex opened 3 years ago

wordlex commented 3 years ago

https://leetcode-cn.com/problems/color-fill-lcci/

wordlex commented 3 years ago
/**
 * @param {number[][]} image
 * @param {number} sr
 * @param {number} sc
 * @param {number} newColor
 * @return {number[][]}
 */
var floodFill = function(image, sr, sc, newColor) {
    const 当前颜色 = image[sr][sc]
    bfs(image,sr,sc)
    return image

    function bfs(image,i,j){
        if(i<0 || j<0 || i>=image.length || j>=image[i].length){
            return
        }
        if(image[i][j] === 当前颜色 && image[i][j] !== newColor){   
            image[i][j] = newColor
            bfs(image,i-1,j) // 上
            bfs(image,i+1,j) // 下
            bfs(image,i,j-1) // 左
            bfs(image,i,j+1) // 右
        }
    }
};