博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode】200. Number of Islands (2 solutions)
阅读量:6312 次
发布时间:2019-06-22

本文共 3453 字,大约阅读时间需要 11 分钟。

Number of Islands

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. 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

Credits:

Special thanks to  for adding this problem and creating all test cases.

 

对每次出现'1'的区域进行计数,同时深度或广度遍历,然后置为'0'。

解法一:非递归dfs

struct Node{    int x;    int y;    Node(int newx, int newy): x(newx), y(newy) {}};class Solution {public:    int numIslands(vector
> &grid) { int ret = 0; if(grid.empty() || grid[0].empty()) return ret; int m = grid.size(); int n = grid[0].size(); for(int i = 0; i < m; i ++) { for(int j = 0; j < n; j ++) { if(grid[i][j] == '1') { dfs(grid, i, j, m, n); ret ++; } } } return ret; } void dfs(vector
> &grid, int i, int j, int m, int n) { stack
stk; Node* rootnode = new Node(i, j); grid[i][j] = '0'; stk.push(rootnode); while(!stk.empty()) { Node* top = stk.top(); if(top->x > 0 && grid[top->x-1][top->y] == '1') {
//check up grid[top->x-1][top->y] = '0'; Node* upnode = new Node(top->x-1, top->y); stk.push(upnode); continue; } if(top->x < m-1 && grid[top->x+1][top->y] == '1') {
//check down grid[top->x+1][top->y] = '0'; Node* downnode = new Node(top->x+1, top->y); stk.push(downnode); continue; } if(top->y > 0 && grid[top->x][top->y-1] == '1') {
//check left grid[top->x][top->y-1] = '0'; Node* leftnode = new Node(top->x, top->y-1); stk.push(leftnode); continue; } if(top->y < n-1 && grid[top->x][top->y+1] == '1') {
//check right grid[top->x][top->y+1] = '0'; Node* rightnode = new Node(top->x, top->y+1); stk.push(rightnode); continue; } stk.pop(); } }};

 

解法二:递归dfs

class Solution {public:    int numIslands(vector
> &grid) { int ret = 0; if(grid.empty() || grid[0].empty()) return ret; int m = grid.size(); int n = grid[0].size(); for(int i = 0; i < m; i ++) { for(int j = 0; j < n; j ++) { if(grid[i][j] == '1') { dfs(grid, i, j, m, n); ret ++; } } } return ret; } void dfs(vector
> &grid, int i, int j, int m, int n) { grid[i][j] = '0'; if(i > 0 && grid[i-1][j] == '1') dfs(grid, i-1, j, m, n); if(i < m-1 && grid[i+1][j] == '1') dfs(grid, i+1, j, m, n); if(j > 0 && grid[i][j-1] == '1') dfs(grid, i, j-1, m, n); if(j < n-1 && grid[i][j+1] == '1') dfs(grid, i, j+1, m, n); }};

转载地址:http://gdhxa.baihongyu.com/

你可能感兴趣的文章
C++策略模式
查看>>
我的友情链接
查看>>
oracle表分区详解
查看>>
网络编程中常见结构体
查看>>
SSL/TLS原理详解
查看>>
Docker 自定义SSH服务镜像
查看>>
JavaScript强化教程 —— Cocos2d-JS自动JSB绑定规则修改
查看>>
configure: error: in `/root/httpd-2.2.11/srclib/apr': c
查看>>
CentOS7搭建Kubernetes-dashboard管理服务
查看>>
buildroot下查找外部编译器通过ext-toolchain-wrapper调用的参数
查看>>
MySQL Replication 主主配置详细说明
查看>>
Linux的任务调度
查看>>
在Android studio中添加jar包方法如下
查看>>
iframe 在ie下面总是弹出新窗口解决方法
查看>>
分享10款漂亮实用的CSS3按钮
查看>>
安装nginx 常见错误及 解决方法
查看>>
Gorun8电子商城
查看>>
在之前链表的基础上改良的链表
查看>>
android编译系统makefile(Android.mk)写法
查看>>
MD5源代码C++
查看>>