标签:
今天出来的新题,已经有大神出结果了,刚开始看还没看懂,就是dfs + 二维渲染 图
http://www.cnblogs.com/easonliu/p/4402077.html
public class Solution { public int numIslands(char[][] grid) { //http://www.cnblogs.com/easonliu/p/4402077.html if(grid==null || grid.length==0 || grid[0].length==0) return 0; int cnt = 0; for(int i=0; i<grid.length; i++){ for(int j=0; j< grid[0].length; j++){ if(grid[i][j]==‘1‘){ // 找到一个尚未被渲染的island cnt++; dfs(grid, i, j);// 感染列岛 } } } return cnt; } public void dfs(char[][] grid, int x, int y){ if(y >= grid[0].length|| x >= grid.length|| x<0 || y<0) return; if(grid[x][y] == ‘1‘) { grid[x][y] =‘X‘; }else return; dfs(grid,x+1,y); dfs(grid,x-1,y); dfs(grid,x,y+1); dfs(grid,x,y-1); } }
标签:
原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4405731.html