[抄题]:

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.

To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.

At the end, return the modified image.

Example 1:

Input:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

忘了dfs的模板格式了,以为退出条件要分开写。但其实退出条件是提前写到一起的。

[一句话思路]:

先染头一个点,形成基础,再四周扩展(还是0 to n-1) 染别的点。不是空中楼阁

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 本题中,原色不想等 不是color的点不能染 。退出条件中,范围在前,特殊情况在后。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

退出条件中,范围在前,特殊情况在后。

[复杂度]:Time complexity: O() Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

退出条件 范围在前

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
//cc
if (image[sr][sc] == newColor) return image; //dfs
dfs(image, sr, sc, image[sr][sc], newColor); //return
return image;
} public void dfs(int[][] image, int sr, int sc, int color, int newColor) {
//exit;
if (sr < 0 || sr >= image.length || sc < 0 || sc>= image[0].length || color != image[sr][sc]) return ; //first color
image[sr][sc] = newColor; //expand
dfs(image, sr + 1, sc, color, newColor);
dfs(image, sr - 1, sc, color, newColor);
dfs(image, sr, sc + 1, color, newColor);
dfs(image, sr, sc - 1, color, newColor);
}
}

最新文章

  1. MyBatis3:SQL映射
  2. 基于Spring Mvc实现的Excel文件上传下载
  3. [Think In Java]基础拾遗4 - 并发
  4. [JavaScript]配置日期选择控件
  5. .NET Framework 高级开发
  6. Majority Element
  7. Maven(1)-安装和配置
  8. IOS深入学习(19)之View object
  9. 【POJ】1084 Square Destroyer
  10. 15分钟弄懂 const 和 #define
  11. Python核心编程(第八章)--条件和循环
  12. SQL Server 性能调优培训引言
  13. mysql基础:数据库的创建,增删改查
  14. 【学习总结】Git学习-参考廖雪峰老师教程二-安装Git
  15. Mac搭建kubernetes dashboard全流程
  16. C#重点内容之:事件(Event)
  17. 持续集成工具jenkins的使用
  18. 20155225 2016-2017-2 《Java程序设计》第一周学习总结
  19. numpy和matplotlib绘制直方图
  20. windows计划任务定时运行synctoy的坑

热门文章

  1. 好强大的页面功能调试(js调试,查找js绑定的事件)值得学习
  2. MyBatis典型的错误org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
  3. ORA-28595: Extproc 代理: DLL 路径无效解决办法
  4. 参数化防SQL注入
  5. Linux yum操作时出现Error: xz compression not available
  6. 解决时间控件input不能选择的问题
  7. if-else 循环嵌套结构
  8. java代码,输入n多个数,求其平均值,虽有重复,但是第二次,我就乱写了
  9. 理解contextmanager
  10. SizeGripStyle 枚举