200. Number of Islands (Graph)
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)的更多相关文章
- 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 用了第一种方式, ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- (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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- SonarQube与Jenkins结合提高代码质量
首先,为什么需要SonarQube? 1.WriteClean Code 1)全局健康报告 2)关注新增的问题 3)强制QualityGate 4)Issue详情及建议- 详情参考:https:/ ...
- 【JEECG技术文档】JEECG 组织机构导入V3.7
1.功能介绍 组织机构导入 提供组织机构模版导入功能,使用户更快速的创建组织机构 要使用组织机构导入功能需要完成以下步骤: 1. 下载模版excel 2. 填写组织机构信息 3. 点击导入-选择文件- ...
- day29-序列化 json、pickle、shelve
1.pickle和JSON序列化 如果我们想用文本文件保存一个 Python 对象怎么操作?这里就涉及到序列化的问题,序列化指的是将原本的字典.列表等内容转换成一个字符串的过程. 2.为什么要使用序列 ...
- Java——如何创建文件夹及文件,删除文件,文件夹
package com.zz; import java.io.File; import java.io.IOException; /** * Java创建文件夹 */ public class Cre ...
- springboot 项目 注意事项
SpringBoot出现下列错误. Your ApplicationContext is unlikely to start due to a @ComponentScan of the defaul ...
- 在CentOS7中利用yum命令安装mysql
在CentOS7中利用yum命令安装mysql 原创 2016年08月31日 10:42:33 标签: mysql / centos 4832 一.说明 我们是在VMware虚拟机上安装的mysql, ...
- 简单实现"回车!=提交"(去除表单的回车即提交)
-------------------------------------------------------------------------------------------------- 实 ...
- delphi调用LUA函数来处理一些逻辑
替同事做了个洛奇英雄传自动染色程序,关于屏幕取色的. 因为里面他对颜色的要求比较复杂,改动也比较大,于是我让他把逻辑写在 lua 脚本里面. uses LUA, LUALIB; function lu ...
- Delphi 不使用自带模板创建服务
program Project1; uses Windows, WinSvc; const ServiceName: pchar = 'SnowWings Service'; DisplayName: ...
- HTML+CSS基础课程二
1.border为系统加边框<style type="text/css"> table tr td,th{border:1px solid #000;} </st ...