classSolution { public: constint dx[4] = {1, 0, 0, -1}; constint dy[4] = {0, 1, -1, 0}; vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) { int currColor = image[sr][sc]; if (currColor == color) { return image; } int n = image.size(), m = image[0].size(); queue<pair<int, int>> que; que.emplace(sr, sc); image[sr][sc] = color; while (!que.empty()) { int x = que.front().first, y = que.front().second; que.pop(); for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) { que.emplace(mx, my); image[mx][my] = color; } } } return image; } };
publicint[][] floodFill(int[][] image, int sr, int sc, int color) { intcurrColor= image[sr][sc]; if (currColor == color) { return image; } intn= image.length, m = image[0].length; Queue<int[]> queue = newLinkedList<int[]>(); queue.offer(newint[]{sr, sc}); image[sr][sc] = color; while (!queue.isEmpty()) { int[] cell = queue.poll(); intx= cell[0], y = cell[1]; for (inti=0; i < 4; i++) { intmx= x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) { queue.offer(newint[]{mx, my}); image[mx][my] = color; } } } return image; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: deffloodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: currColor = image[sr][sc] if currColor == color: return image n, m = len(image), len(image[0]) que = collections.deque([(sr, sc)]) image[sr][sc] = color while que: x, y = que.popleft() for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]: if0 <= mx < n and0 <= my < m and image[mx][my] == currColor: que.append((mx, my)) image[mx][my] = color return image
int** floodFill(int** image, int imageSize, int* imageColSize, int sr, int sc, int color, int* returnSize, int** returnColumnSizes) { int n = imageSize, m = imageColSize[0]; *returnSize = n; for (int i = 0; i < n; i++) { (*returnColumnSizes)[i] = m; } int currColor = image[sr][sc]; if (currColor == color) { return image; } int que[n * m][2]; int l = 0, r = 0; que[r][0] = sr, que[r++][1] = sc; image[sr][sc] = color; while (l < r) { int x = que[l][0], y = que[l++][1]; for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) { que[r][0] = mx, que[r++][1] = my; image[mx][my] = color; } } } return image; }
publicint[][] floodFill(int[][] image, int sr, int sc, int color) { intcurrColor= image[sr][sc]; if (currColor != color) { dfs(image, sr, sc, currColor, color); } return image; }
publicvoiddfs(int[][] image, int x, int y, int currColor, int color) { if (image[x][y] == currColor) { image[x][y] = color; for (inti=0; i < 4; i++) { intmx= x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < image.length && my >= 0 && my < image[0].length) { dfs(image, mx, my, currColor, color); } } } } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: deffloodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: n, m = len(image), len(image[0]) currColor = image[sr][sc]
defdfs(x: int, y: int): if image[x][y] == currColor: image[x][y] = color for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]: if0 <= mx < n and0 <= my < m and image[mx][my] == currColor: dfs(mx, my)
voiddfs(int** image, int x, int y, int currColor, int color) { if (image[x][y] == currColor) { image[x][y] = color; for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < n && my >= 0 && my < m) { dfs(image, mx, my, currColor, color); } } } }
int** floodFill(int** image, int imageSize, int* imageColSize, int sr, int sc, int color, int* returnSize, int** returnColumnSizes) { n = imageSize, m = imageColSize[0]; *returnSize = n; for (int i = 0; i < n; i++) { (*returnColumnSizes)[i] = m; } int currColor = image[sr][sc]; if (currColor != color) { dfs(image, sr, sc, currColor, color); } return image; }
var ( dx = []int{1, 0, 0, -1} dy = []int{0, 1, -1, 0} )
funcfloodFill(image [][]int, sr int, sc int, color int) [][]int { currColor := image[sr][sc] if currColor != color { dfs(image, sr, sc, currColor, color) } return image }
funcdfs(image [][]int, x, y, currColor, color int) { if image[x][y] == currColor { image[x][y] = color for i := 0; i < 4; i++ { mx, my := x + dx[i], y + dy[i] if mx >= 0 && mx < len(image) && my >= 0 && my < len(image[0]) { dfs(image, mx, my, currColor, color) } } } }
复杂度分析
时间复杂度:O(n\times m),其中 n 和 m 分别是二维数组的行数和列数。最坏情况下需要遍历所有的方格一次。
空间复杂度:O(n\times m),其中 n 和 m 分别是二维数组的行数和列数。主要为栈空间的开销。