题目:

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

这个题目的意思就是1表示陆地,0表示是水,问你陆地有多少块,其实就是求最大联通块的数目。

可以想到用深搜去解决它。对于每一块陆地1进行Dfs,Dfs过了以后就把它标记为0,这样全部走一遍就知道有多少个联通块了。

接下来就是代码实现了,这次的代码还是遇见了一点小问题,先贴出有问题的代码:

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int islandNum=0;
        int row=grid.size();
        int column=grid[0].size();
        for(int i=0;i<row;i++)
            for(int j=0;j<column;j++)
            {
                if(grid[i][j]=='1')
                {
                    Dfs(grid,i,j);
                    islandNum++;
                }
            }
        return islandNum;
    }
    void Dfs(vector<vector<char>>& g,int r,int c)
    {
        if(g[r][c]=='0') return;
        g[r][c]='0';
        if(r<g.size()-1)
            Dfs(g,r+1,c);
        if(c<g[0].size()-1)
            Dfs(g,r,c+1);
    }
};

报的错误是:

Runtime Error Message:reference binding to null pointer of type 'struct value_type'
Last executed input:[]
在网上查了,这个问题主要就是没有进行边界条件的处理,也就是对输入[]没有进行合适的处理。
这个代码还有另一个错误就是我只想着往左走和往下走,应该需要上下左右都走,然后注意条件的判断。
正确提交的代码如下:
 class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int islandNum=;
if(grid.size()==) return ;
if(grid[].size()==) return ;
int row=grid.size();
int column=grid[].size();
for(int i=;i<row;i++)
for(int j=;j<column;j++)
{
if(grid[i][j]=='')
{
Dfs(grid,i,j);
islandNum++;
}
}
return islandNum;
}
void Dfs(vector<vector<char>>& g,int r,int c)
{
if(g[r][c]=='') return;
g[r][c]='';
if(r<g.size()-)
Dfs(g,r+,c);
if(c<g[].size()-)
Dfs(g,r,c+);
if(r>)
Dfs(g,r-,c);
if(c>)
Dfs(g,r,c-);
}
};
 

leetcode题解 200. Number of Islands(其实就是一个深搜)的更多相关文章

  1. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  2. 【LeetCode】200. Number of Islands (2 solutions)

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

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

  4. 【刷题-LeetCode】200 Number of Islands

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  5. 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 用了第一种方式, ...

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

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

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

  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. 各版本最新的Visual C++可再发行组件包(Redistributable Package)下载和合集

    Microsoft Visual C++ 2005 Redistributable Package (x86):Microsoft Visual C++ 2005 可再发行组件包 (x86):http ...

  2. 怎样从外网访问内网DB2数据库

    外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...

  3. poj2115 C Looooops(exgcd)

    poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...

  4. UnicodeMath编码教程

    目录 1. 简介 2. 编码简单数学表达式 2.1 分数 2.2 上标和下标 2.3 空白(空格)字符使用 3. 编码其他数学公式 3.1 open/close分隔符 关于大括号方程组(cases) ...

  5. 【搬运】Wget 命令详解

    用过 Linux 系统的对于 wget 不陌生吧,从网上下载资源等操作都是少不了它,它体积小但功能集全,支持 FTP HTTP HTTPS 协议下载方式,支持断点续传 代理服务器. 现在 Window ...

  6. Android hide the app icon but show the icon most left

    ActionBar actionBar = getActionBar(); actionBar.setIcon(new ColorDrawable(getResources().getColor(an ...

  7. java IO和NIO区别

    面向流与面向缓冲 Java NIO和IO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的. Java IO面向流意味着每次从流中读一个或多个字节,直至读取所有字节,它们没有被缓存在任何地方 ...

  8. 微信小程序,加载更多

    html <!-- 头部 --> <view class='tab'> <view class="tab-new {{selected_new?'active' ...

  9. Axure 交互样式和选项组的设置

    1.点击元件,点击属性,点击选中,点击图片,选择相应的图片,选中功能是当点击这个元件的时候,元件的变成相应的图片,然后再设置鼠标单击时的动作是选中,就可以实现选中某个元件的时候,元件会变成其他的图片. ...

  10. linux软件管理之源码包管理

    源码包管理tarball ====================================================================================tar ...