-
733. Flood Fill javaLeetCode_Study_Plan/Algorithm 2022. 12. 22. 14:46
https://leetcode.com/problems/flood-fill/description/
class Solution { int[][] image; int m; int n; int newColor; int oldColor; boolean [][] visited; public int[][] floodFill(int[][] image, int sr, int sc, int color) { this.m = image.length; this.n = image[0].length; this.image = image; this.oldColor = image[sr][sc]; this.newColor = color; this.visited = new boolean[this.m][this.n]; dfs(sr, sc); return this.image; } public void dfs(int i, int j) { if (go_outside(i, j)) return; if (this.visited[i][j]) return; this.visited[i][j] = true; if (this.image[i][j] != this.oldColor) return; this.image[i][j] = this.newColor; dfs(i+1, j); dfs(i, j+1); dfs(i-1, j); dfs(i, j-1); } public boolean go_outside(int i, int j) { if (i < 0 || i >= m || j < 0 || j >= n) { return true; } return false; } }
'LeetCode_Study_Plan > Algorithm' 카테고리의 다른 글
🎵 617. Merge Two Binary Trees java (0) 2022.12.26 695. Max Area of Island java (0) 2022.12.22 3. Longest Substring Without Repeating Characters java (0) 2022.12.21 567. Permutation in String java (0) 2022.12.21 🎵 19. Remove Nth Node From End of List java (0) 2022.12.21