Code Monkey home page Code Monkey logo

Comments (2)

triangleCZH avatar triangleCZH commented on August 15, 2024

@lei-hsia 在主函数的if ()不是唯一进入递归的方法,辅函数里面也有dfs(grid, x, y), 那么这个辅函数调用自己之前也需要检测 == 0的case。如果不这么做的话,会有以下问题:
假设[[1,1,1,1,1]],我们从dfs(grid, 0,0)开始,他会进入递归去做dfs(grid, 0, 1),dfs(grid,0,1)反过来又会去做dfs(grid,0,0),这里只有在辅函数内部检测过 grid[0][0] == 0 才能停下,否则两个连续的island会无限相互call。辅函数中把grid[r][c] 先设置成0就是为了标记已检测的岛屿,配合if grid[][] == 0防止被二次检测

from leetcode_101.

lei-hsia avatar lei-hsia commented on August 15, 2024

赞👍. 懂了,其实最重要的地方就是 dfs中也会调用dfs

from leetcode_101.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.