All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0701_0800.s0733_flood_fill.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0701_0800.s0733_flood_fill;

// #Easy #Array #Depth_First_Search #Breadth_First_Search #Matrix

public class Solution {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int o = image[sr][sc];
        helper(image, sr, sc, newColor, o);
        return image;
    }

    private void helper(int[][] img, int r, int c, int n, int o) {
        if (r >= img.length
                || c >= img[0].length
                || r < 0
                || c < 0
                || img[r][c] == n
                || img[r][c] != o) {
            return;
        }
        img[r][c] = n;
        helper(img, r + 1, c, n, o);
        helper(img, r - 1, c, n, o);
        helper(img, r, c + 1, n, o);
        helper(img, r, c - 1, n, o);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy