文章最前: 我是Octopus,這個名字來源於我的中文名--章魚;我熱愛編程、熱愛算法、熱愛開源。所有源碼在我的個人github ;這博客是記錄我學習的點點滴滴,如果您對 Python、Java、AI、算法有興趣,可以關注我的動態,一起學習,共同進步。
相關文章:
- LeetCode:55. Jump Game(跳遠比賽)
- Leetcode:300. Longest Increasing Subsequence(最大增長序列)
- LeetCode:560. Subarray Sum Equals K(找出數組中連續子串和等於k)
文章目錄:
源碼地址:https://github.com/zhangyu345293721/leetcode
題目描述:
有一幅以二維整數數組表示的圖畫,每一個整數表示該圖畫的像素值大小,數值在 0 到 65535 之間。
給你一個座標 (sr, sc) 表示圖像渲染開始的像素值(行 ,列)和一個新的顏色值 newColor,讓你重新上色這幅圖像。
爲了完成上色工作,從初始座標開始,記錄初始座標的上下左右四個方向上像素值與初始座標相同的相連像素點,接着再記錄這四個方向上符合條件的像素點與他們對應四個方向上像素值與初始座標相同的相連像素點,……,重複該過程。將所有有記錄的像素點的顏色值改爲新的顏色值。最後返回經過上色渲染後的圖像。
示例 1:
輸入:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
輸出: [[2,2,2],[2,2,0],[2,0,1]]
解析:
在圖像的正中間,(座標(sr,sc)=(1,1)),
在路徑上所有符合條件的像素點的顏色都被更改成2。
注意,右下角的像素沒有更改爲2,
因爲它不是在上下左右四個方向上與初始點相連的像素點。
注意:
image 和 image[0] 的長度在範圍 [1, 50] 內。
給出的初始點將滿足 0 <= sr < image.length 和 0 <= sc < image[0].length。
image[i][j] 和 newColor 表示的顏色值在範圍 [0, 65535]內。
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
java實現方法1:
/**
* @param image 圖片二維數組
* @param sr sr輸入
* @param sc sc輸入
* @param newColor 新顏色
* @return 二維數組
*/
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
return floodFillMethod(image, sr, sc, image[sr][sc], newColor);
}
/**
* 圖片變換
*
* @param image 二維數組
* @param sr sr變換
* @param sc sc變換
* @param oldColor 老顏色
* @param newColor 新顏色
* @return 二維數組
*/
public int[][] floodFillMethod(int[][] image, int sr, int sc, int oldColor, int newColor) {
int rowLength = image.length;
int colmnLength = image[0].length;
if (sr < 0 || sr >= rowLength || sc < 0 || sc >= colmnLength || image[sr][sc] != oldColor || image[sr][sc] == newColor) {
return image;
}
image[sr][sc] = newColor;
floodFillMethod(image, sr + 1, sc, oldColor, newColor);
floodFillMethod(image, sr, sc + 1, oldColor, newColor);
floodFillMethod(image, sr - 1, sc, oldColor, newColor);
floodFillMethod(image, sr, sc - 1, oldColor, newColor);
return image;
}
時間複雜度:O(n)
空間複雜度: O(n)
python實現方法1:
# encoding='utf-8'
'''
author:zhangyu
date:2020.1.31
description:找到兩個數組中的交集
'''
from typing import List
def flood_fill_method(image: List[List[int]], sr: int, sc: int, old_color: int, new_color: int) -> List[List[int]]:
'''
數組轉變
Args:
image:二維數組
sr: 輸入sr
sc: 輸入sc
old_color:老顏色
new_color: 新顏色
Returns:
變換後的數組
'''
row = len(image)
col = len(image[0])
if (sr < 0 or sr >= row) or (sc < 0 or sc >= col) or image[sr][sc] != old_color or image[sr][sc] == new_color:
return image
image[sr][sc] = new_color
flood_fill_method(image, sr + 1, sc, old_color, new_color)
flood_fill_method(image, sr, sc + 1, old_color, new_color)
flood_fill_method(image, sr - 1, sc, old_color, new_color)
flood_fill_method(image, sr, sc - 1, old_color, new_color)
return image
def flood_fill(image: List[List[int]], sr: int, sc: int, new_color: int) -> List[List[int]]:
return flood_fill_method(image, sr, sc, image[sr][sc], new_color)
if __name__ == '__main__':
image = [[1, 1, 1], [1, 1, 0], [1, 0, 1]]
sr = 1
sc = 1
new_color = 2
old_color = flood_fill(image, sr, sc, new_color);
print(old_color)
時間複雜度:O(n)
空間複雜度: O(n)