GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-2/java/733_Flood_Fill.java at master · casually-PYlearner/leetcode-2 Expand file tree/ Copy path733_Flood_Fill.javaMore file actions 48 lines (44 loc) · 1.62 KB / Copy path733_Flood_Fill.javaFile metadata and controls48 lines (44 loc) · 1.62 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 class Solution { /*public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { if (image[sr][sc] == newColor) return image; dfs(image, sr, sc, image[sr][sc], newColor); return image; } private void dfs(int[][] image, int r, int c, int color, int newColor) { // Recursively DFS if (image[r][c] == color) { image[r][c] = newColor; if (r - 1 >= 0) dfs(image, r - 1, c, color, newColor); if (r + 1 < image.length) dfs(image, r + 1, c, color, newColor); if (c - 1 >= 0) dfs(image, r, c - 1, color, newColor); if (c + 1 < image[0].length) dfs(image, r, c + 1, color, newColor); } }*/ public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { Queue<Node> queue = new LinkedList<Node>(); int color = image[sr][sc]; if (color == newColor) return image; queue.add(new Node(sr, sc)); // BFS with queue while (!queue.isEmpty()) { Node curr = queue.remove(); int r = curr.r, c = curr.c; if (image[r][c] == color) { image[r][c] = newColor; if (r - 1 >= 0) queue.add(new Node(r - 1, c)); if (r + 1 < image.length) queue.add(new Node(r + 1, c)); if (c - 1 >= 0) queue.add(new Node(r, c - 1)); if (c + 1 < image[0].length) queue.add(new Node(r, c + 1)); } } return image; } class Node { int r; int c; public Node(int r, int c) { this.r = r; this.c = c; } } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |