Skip to content

Instantly share code, notes, and snippets.

@techsin
Created June 12, 2020 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techsin/9e289c1564f56765567cd95ff158b732 to your computer and use it in GitHub Desktop.
Save techsin/9e289c1564f56765567cd95ff158b732 to your computer and use it in GitHub Desktop.
find islands leetcode
var numIslands = function(grid) {
let islands = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] == 1) {
markRecur(i, j, grid);
islands++;
}
}
}
return islands;
};
function markRecur(x, y, grid) {
grid[Number(x)][Number(y)] = 'x';
if (grid[x-1] && grid[x-1][y] == 1) markRecur(x-1, y, grid);
if (grid[x+1] && grid[x+1][y] == 1) markRecur(x+1, y, grid);
if (grid[x][y+1] && grid[x][y+1] == 1) markRecur(x, y+1, grid);
if (grid[x][y-1] && grid[x][y-1] == 1) markRecur(x, y-1, grid);
}
@techsin
Copy link
Author

techsin commented Jun 12, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment