Skip to content

feat: add lcci color fill java solution #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lcci/08.10.Color Fill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
```

### ...
Expand Down
24 changes: 23 additions & 1 deletion lcci/08.10.Color Fill/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,29 @@ to the starting pixel.</pre>
### 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);
}
}
}
```

### ...
Expand Down
23 changes: 23 additions & 0 deletions lcci/08.10.Color Fill/Solution.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}