From 017287e8c38e9b4c2d31e9fd8318de1d09729c8f Mon Sep 17 00:00:00 2001 From: J-Cod3r Date: Sat, 4 Jul 2020 12:34:26 +0000 Subject: [PATCH] feat: add lcci color fill java solution --- lcci/08.10.Color Fill/README.md | 24 +++++++++++++++++++++++- lcci/08.10.Color Fill/README_EN.md | 24 +++++++++++++++++++++++- lcci/08.10.Color Fill/Solution.java | 23 +++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 lcci/08.10.Color Fill/Solution.java diff --git a/lcci/08.10.Color Fill/README.md b/lcci/08.10.Color Fill/README.md index 5c2780582c8b5..96b1687ac30e4 100644 --- a/lcci/08.10.Color Fill/README.md +++ b/lcci/08.10.Color Fill/README.md @@ -42,7 +42,29 @@ sr = 1, sc = 1, newColor = 2 ```java - +class Solution { + public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { + int oldColor = image[sr][sc]; + dfs(image, sr, sc, oldColor, newColor); + return image; + } + + private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) { + if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length) { + return; + } + + int color = image[sr][sc]; + if (color != newColor && color == oldColor) { + image[sr][sc] = newColor; + // up down left right + dfs(image, sr, sc + 1, oldColor, newColor); + dfs(image, sr, sc - 1, oldColor, newColor); + dfs(image, sr + 1, sc, oldColor, newColor); + dfs(image, sr - 1, sc, oldColor, newColor); + } + } +} ``` ### ... diff --git a/lcci/08.10.Color Fill/README_EN.md b/lcci/08.10.Color Fill/README_EN.md index 9861a40acb5b0..526eb2442a10a 100644 --- a/lcci/08.10.Color Fill/README_EN.md +++ b/lcci/08.10.Color Fill/README_EN.md @@ -56,7 +56,29 @@ to the starting pixel. ### Java ```java - +class Solution { + public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { + int oldColor = image[sr][sc]; + dfs(image, sr, sc, oldColor, newColor); + return image; + } + + private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) { + if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length) { + return; + } + + int color = image[sr][sc]; + if (color != newColor && color == oldColor) { + image[sr][sc] = newColor; + // up down left right + dfs(image, sr, sc + 1, oldColor, newColor); + dfs(image, sr, sc - 1, oldColor, newColor); + dfs(image, sr + 1, sc, oldColor, newColor); + dfs(image, sr - 1, sc, oldColor, newColor); + } + } +} ``` ### ... diff --git a/lcci/08.10.Color Fill/Solution.java b/lcci/08.10.Color Fill/Solution.java new file mode 100644 index 0000000000000..c49d8ef556e60 --- /dev/null +++ b/lcci/08.10.Color Fill/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { + int oldColor = image[sr][sc]; + dfs(image, sr, sc, oldColor, newColor); + return image; + } + + private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) { + if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length) { + return; + } + + int color = image[sr][sc]; + if (color != newColor && color == oldColor) { + image[sr][sc] = newColor; + // up down left right + dfs(image, sr, sc + 1, oldColor, newColor); + dfs(image, sr, sc - 1, oldColor, newColor); + dfs(image, sr + 1, sc, oldColor, newColor); + dfs(image, sr - 1, sc, oldColor, newColor); + } + } +} \ No newline at end of file