733. 图像渲染
递归解法
执行用时:84 ms, 在所有 JavaScript 提交中击败了94.96% 的用户 内存消耗:39.9 MB, 在所有 JavaScript 提交中击败了56.30% 的用户
var floodFill = function (image, sr, sc, newColor) {// 如果目标颜色与图象原颜色相等,则直接返回,否则出现死循环if (newColor === image[sr][sc]) return image;floodFillRecursion(image, sr, sc, newColor, image[sr][sc]);return image;};var floodFillRecursion = function (image, sr, sc, newColor, oldColor) {// 坐标越界判断if (!(sr >= 0 && sr < image.length && sc >= 0 && sc < image[0].length)) {return;}// 是否为目标颜色,如果不是,跳出递归if (image[sr][sc] === oldColor) {image[sr][sc] = newColor;} else {return;}for (let d of dire) {// 递归调用上下左右floodFillRecursion(image, sr + d[0], sc + d[1], newColor, oldColor);}}// 定义上下左右方向var dire = [[0, 1],[0, -1],[1, 0],[-1, 0]]
