Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically(意思是,如果仅仅是斜角都为1,不是ajacent land). You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

思路:找到没有标志的1,表示找到了一个island,然后找它的连通图,把这个连通图里的1都设置标志。

class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int hight = grid.size();
if(hight == ) return ;
int width = grid[].size();
int ret = ; for(int i = ; i < hight; i++){
for(int j = ; j < width; j++){
if(grid[i][j]=='') continue; //find a land, continue to find land at its side
ret++;
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
}
return ret;
} void dfs(vector<vector<char>>& grid, int i, int j){
int hight = grid.size();
int width = grid[].size(); if(i>=hight || j>= width || i< || j<) return;
if(grid[i][j]=='') return; //find a land, continue to find land at its side
grid[i][j] = ''; //set flag
dfs(grid, i+, j);
dfs(grid, i, j+);
dfs(grid, i-, j);
dfs(grid, i, j-);
}
};

200. Number of Islands (Graph)的更多相关文章

  1. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  2. 200. Number of Islands

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  3. Leetcode 200. number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  4. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  5. [LeetCode] 200. Number of Islands 解题思路

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  6. (BFS/DFS) leetcode 200. Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  7. 200. Number of Islands(DFS)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. leetcode题解 200. Number of Islands(其实就是一个深搜)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  9. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. [Python]实践:实现探测Web服务质量

    来源:Python 自动化运维 技术与最佳实践 HTTP服务是最流行的互联网应用之一,服务质量的好坏关系到用户体验以及网站的运营服务水平,最常用的有两个标准:1.服务的可用性,比是否处于正常提供服务状 ...

  2. weblogic stage更改不马上生效

    在主机上domins中存在stage目录,且删除相关文件后页面访问报404 mc11>home/ap/user/domins/user/stage/project/project.war/js/ ...

  3. githup创建新java项目

    1.在githup中创建仓库 2.import创建的地址到本地文件d:/mygit 3.在d:/mygit中创建eclipse项目 3.在eclipse中team-->push to branc ...

  4. Linux FACL(文件访问控制列表)

    文件有三种权限 属主权限   属组权限  其他权限 现在有这样一个场景,用户 A 想把文件共享给不是同组内用户 B ,而又不想修改其他权限,这时候 FACL 就起作用了 FACL可以给文件添加一个拓展 ...

  5. react-native-echarts 安卓版打包后,部分手机图表不显示问题

    1. 找到  node_modules\native-echarts\src\components\Echarts\tpl.html 文件 ,把它复制到 (android\app\src\main\a ...

  6. WDA-3-ALV查询

    主要是梳理下WebDynpro For ABAP开发过程: 1.创建WebDynpro组件 2.创建WebDynpro应用 1.创建WebDynpro组件 1.1创建 路径:选择Package--&g ...

  7. DOM节点的增删改查以及class属性的操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. ORM查询api

    下面的方法都是对查询的结果进行出理:比如objects.filter.values()... 1)values(*field):返回一个可迭代的字典序列<QuerySet: [{name='小王 ...

  9. CAS无锁技术

    前言:关于同步,很多人都知道synchronized,Reentrantlock等加锁技术,这种方式也很好理解,是在线程访问的临界区资源上建立一个阻塞机制,需要线程等待 其它线程释放了锁,它才能运行. ...

  10. C#开发VS LUA开发

    一个游戏公司,决定开始用U3D做一款新游戏,这个游戏类型从来没做过. 如果没有一个成熟的游戏框架,那么从头撸起. 是一开始就将LUA热更新考虑进来呢 还是先做成纯C#的框架呢? 考虑因素:游戏逻辑如果 ...