原题链接:  http://oj.leetcode.com/problems/surrounded-regions/  
这个题目用到的方法是图形学中的一个常用方法: Flood fill算法 ,其实就是从一个点出发对周围区域进行目标颜色的填充。背后的思想就是把一个矩阵看成一个图的结构,每个点看成结点,而边则是他上下左右的相邻点,然后进行一次广度或者深度优先搜索。
接下来我们看看这个题如何用 Flood fill算法 来解决。首先根据题目要求,边缘上的'O'是不需要填充的,所以我们的办法是对上下左右边缘做 Flood fill算法 ,把所有边缘上的'O'都替换成另一个字符,比如'#'。接下来我们知道除去被我们换成'#'的那些顶点,剩下的所有'O'都应该被替换成'X',而'#'那些最终应该是还原成'O',如此我们可以做最后一次遍历,然后做相应的字符替换就可以了。复杂度分析上,我们先对边缘做 Flood fill算法 ,因为只有是'O'才会进行,而且会被替换成'#',所以每个结点改变次数不会超过一次,因而是O(m*n)的复杂度,最后一次遍历同样是O(m*n),所以总的时间复杂度是O(m*n)。空间上就是递归栈(深度优先搜索)或者是队列(广度优先搜索)的空间,同时存在的空间占用不会超过O(m+n)(以广度优先搜索为例,每次队列中的结点虽然会往四个方向拓展,但是事实上这些结点会有很多重复,假设从中点出发,可以想象最大的扩展不会超过一个菱形,也就是n/2*2+m/2*2=m+n,所以算法的空间复杂度是O(m+n))。代码如下:

public void solve(char[][] board) {if(board==null || board.length<=1 || board[0].length<=1)return;for(int i=0;i<board[0].length;i++){fill(board,0,i);fill(board,board.length-1,i);}for(int i=0;i<board.length;i++){fill(board,i,0);fill(board,i,board[0].length-1);}for(int i=0;i<board.length;i++){for(int j=0;j<board[0].length;j++){if(board[i][j]=='O')board[i][j]='X';else if(board[i][j]=='#')board[i][j]='O';                }}
}
private void fill(char[][] board, int i, int j)
{if(board[i][j]!='O')return;board[i][j] = '#';LinkedList<Integer> queue = new LinkedList<Integer>();int code = i*board[0].length+j;queue.offer(code);while(!queue.isEmpty()){code = queue.poll();int row = code/board[0].length;int col = code%board[0].length;if(row>0 && board[row-1][col]=='O'){queue.offer((row-1)*board[0].length+col);board[row-1][col]='#';}if(row<board.length-1 && board[row+1][col]=='O'){queue.offer((row+1)*board[0].length+col);board[row+1][col]='#';}if(col>0 && board[row][col-1]=='O'){queue.offer(row*board[0].length+col-1);board[row][col-1]='#';}if(col<board[0].length-1 && board[row][col+1]=='O'){queue.offer(row*board[0].length+col+1);board[row][col+1]='#';}            }
}

可以看到上面代码用的是广度优先搜索,用一个队列来维护,当然也可以用深度优先搜索,但是如果使用递归,会发现LeetCode过不了,这是因为在图形中通常图片(或者说这里的矩阵)一般会很大,递归很容易导致栈溢出,所以即使要用深度优先搜索,也最好使用非递归的实现方式哈。

Surrounded Regions -- LeetCode相关推荐

  1. 【BFS】LeetCode 130. Surrounded Regions

    LeetCode 130. Surrounded Regions Solution1:我的答案 利用bfs解决,超过98.7%的答案,还不错.关键是简单易懂! class Solution { pub ...

  2. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  3. leetcode @python 130. Surrounded Regions

    题目链接 https://leetcode.com/problems/surrounded-regions/ 题目原文 Given a 2D board containing 'X' and 'O', ...

  4. [LeetCode] 130. Surrounded Regions Java

    题目:Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A ...

  5. LeetCode Surrounded Regions(floodfill算法)

    问题:一个由X,O字符填充的二维数组,问由O包围起来的X,其中O与边界的不计算在内. 思路:floodfill算法 ,先求出O构成的连通,将这些剔除后,就是由O包围起来的X 具体代码参考: https ...

  6. LeetCode: Surrounded Regions [130]

    [题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...

  7. 利用BFS和DFS解决 LeetCode 130: Surrounded Regions

    问题来源 此题来源于LEETCODE,具体问题详见下面的链接 https://leetcode.com/problems/surrounded-regions/description/ 问题简述 给定 ...

  8. LeetCode OJ - Surrounded Regions

    题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...

  9. [LeetCode] Surrounded Regions, Solution

    http://fisherlei.blogspot.com/search?q=Surrounded+Region 特别说一下,里面实现bfs的code挺有意思的 int k =0; 20: while ...

最新文章

  1. 开发日记-20190516 关键词 MVVM-代码浏览结束
  2. 台湾国立大学郭彦甫Matlab教程笔记(16) 数值微分 numerical differentiation
  3. 【最佳实践】Elasticsearch Snapshot 备份的使用方法
  4. 由控制台输入年龄-不同类型不能直接比较
  5. go uintptr unsafe Pointer offset() 的使用
  6. python官方的扩展索引_Python列表操作与深浅拷贝(6)——列表索引、查询、修改、扩展...
  7. 11.29 广州国际设计周
  8. linux用call调存储过程,存储过程调用其他模式的存储过程需要注意的地方
  9. 最优化读书笔记R(一)
  10. java基础-软件简述
  11. Android动画总结
  12. 收不到oracle注册邮件或无法登录
  13. HTTP: CDN缓存机制
  14. 存储容量byte 流量单位byte
  15. meaven install提示系统资源不足
  16. 计算机新生导学课心得,新生导学课论文_新生导学课感想_大一新生导学课心得...
  17. .NET JWT Token验证
  18. DNF史诗计算机最新版,dnf全职业史诗装备搭配计算器2020
  19. 解决tomcat在pycharm中的初始化报错“ Failed to transfer file”“could not create folder”“(Permission denied)”
  20. 【数据分析与挖掘(一)】笔试题汇总(附答案)

热门文章

  1. foxmail中无法设置qq邮箱
  2. 工具︱ Web3加密浏览器Brave有什么特别之处?
  3. 几种常用用户行为分析模型简述
  4. 如何在Linux上安装QQ
  5. phpstorm2022连接数据库
  6. 特殊分布律篇1——泊松分布
  7. 任务 04、Midjourney提示词使用初指南:AI绘画不再难
  8. 天钰原装正品FR9608供应,同步降压DC/DC提供4.5V至28V宽输入的转换器
  9. python for循环语句怎么写
  10. react-native系列(6)组件篇: ScrollView滚屏及滚屏加载