-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloodFill.java
More file actions
25 lines (23 loc) · 906 Bytes
/
Copy pathFloodFill.java
File metadata and controls
25 lines (23 loc) · 906 Bytes
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
// 733. Flood Fill
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int iniColor = image[sr][sc];
int[][] ans = image;
int[] delRow = {-1, 0, +1, 0};
int[] delCol = {0, +1, 0, -1};
dfs(sr, sc, ans, image, color, delRow, delCol, iniColor);
return ans;
}
private void dfs(int sr, int sc,int[][] ans,int[][] image, int color, int[] delRow, int[] delCol, int iniColor){
ans[sr][sc] = color;
int m = image.length;
int n = image[0].length;
for(int i = 0 ; i < 4; i++){
int nrow = sr + delRow[i];
int ncol = sc + delCol[i];
if(nrow >= 0 && nrow < m && ncol >= 0 && ncol < n && image[nrow][ncol] == iniColor && ans[nrow][ncol] != color){
dfs(nrow, ncol, ans, image, color, delRow, delCol, iniColor);
}
}
}
}