题目:

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. 软件工程---UML理解

    1.依赖关系和关联关系 1.1依赖关系是调用关系,其又分为全局依赖.参数依赖.局部依赖 1.2关联关系是结构上的关系,按照关联的强弱又具体分为关联关系.聚合关系(整体和部分的组成关系.whole-pa ...

  2. 剑指offer(14)链表中倒数第K个节点

    题目描述 输入一个链表,输出该链表中倒数第k个节点. 题目分析 用两个指针来跑,两个指针中间相距k-1个节点,第一个指针先跑,跑到了第k个节点时,第二个指针则是第一个节点. 这时候两个一起跑.当第一个 ...

  3. 剑指offer(35)数组中的逆序对

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...

  4. Mac OS X 避免产生临时文件 .DS_Store

    参考: 删除Mac中所有 .DS_Store 隐藏文件 Mac OS X 避免产生临时文件 .DS_Store .DS_Store 隐藏文件保存针对目录的特殊信息和设置配置,例如查看方式,图标大小以及 ...

  5. python requests用法总结

    requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到.可以说,Requests 完全满足如今网络的需求 本文全部来源于官方文档 http://docs ...

  6. selenium配置文件定位元素

    之前的写的selenium的定位元素进行测试的代码,现在一运行就报找不到元素了,之前运行的好好的. 我查看网站源码后,发现网站元素确实是变了,原来的定位的xpath代码压根全部找不到了,于是 想着,以 ...

  7. hdoj4685

    数据: /*999993 43 1 2 42 2 32 3 4*/ #include <iostream> #include <cstdio> #include <cma ...

  8. 随机森林RF、XGBoost、GBDT和LightGBM的原理和区别

    目录 1.基本知识点介绍 2.各个算法原理 2.1 随机森林 -- RandomForest 2.2 XGBoost算法 2.3 GBDT算法(Gradient Boosting Decision T ...

  9. mongodump and mongorestore

    mongoexport和mongoimport只能导出/导入某个特定集合 1 mongoexport bin目录下 ./mongoexport <hostname><:port> ...

  10. 原生JS获取DOM 节点到浏览器顶部的距离或者左侧的距离

    关于js获取dom 节点到浏览器顶/左部的距离,Jquery里面有封装好的offset().top/offset().left,只到父级的顶/左部距离position().top/position() ...