题目:

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. [转载]lib和dll文件的区别和联系

    出处:https://blog.csdn.net/weiaipan1314/article/details/52252478 什么是lib文件,lib和dll的关系如何 (2008-04-18 19: ...

  2. 石家庄铁道大学课程信息管理系统(javaWeb+servlet+Mysql)

    实现网页版的课程管理系统,具有增删改查的功能. 1.首先连接数据库,具体数据库的使用及如何连接eclipse,参考     https://blog.csdn.net/lrici/article/de ...

  3. json字符串 转Java List 简单方法

    JSONArray jsonArr = JSONArray.fromObject(jsonStr); List<Map<String,Object>> listMap = (L ...

  4. *(volatile int *)解读

    #def ine reg_gpio_ctrl *(volatile int *)(ToVirtual(GPIO_REG_CTRL)) #define A (*(volatile unsigned lo ...

  5. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  6. shadows

    http://blog.51cto.com/11975865/2308030 https://www.jianshu.com/p/247cdbabf389 https://baijiahao.baid ...

  7. freeswitch编译安装,初探, 以及联合sipgateway, webrtc server的使用场景。

    本文主要记录freeswitch学习过程. 一 安装freeswitch NOTE 以下两种安装方式,再安装的过程中遇到了不少问题,印象比较深刻的就是lua库找到不到这个问题.这个问题发生在make ...

  8. JAVA中的值传递和引用传递问题

    这是个老生常谈的问题了,引起过无数争论,但可以说一直没有一个令人满意的回答. 有人总结过: 对象是按引用传递的 Java 应用程序有且仅有的一种参数传递机制,即按值传递 按值传递意味着当将一个参数传递 ...

  9. 《SQL 基础教程》第二章:查询基础

    这一章的结构如下: SELECT 语句基础 算术运算符和比较运算符 逻辑运算符 SELECT 语句可用于查询数据,并且可以设定条件来查询具有特定值的记录.条件的设定就需要算数运算符.比较运算符和逻辑运 ...

  10. CAM(Content Addressable Memory)介绍

    CAM是一种特殊的存储器.所谓CAM,即内容寻址存储器.CAM存储器在其每个存储单元都包含了一个内嵌的比较逻辑,写入CAM的数据会和其内部存储的每一个数据进行比较,并返回与端口数据相同的所有内部数据的 ...