Dodomeki
Sign in

31. Islands in a Grid

Easy

In a matrix of characters, 1 represents land and 0 represents water. Return how many islands there are, considering only cells that share a side as connected. An island is a maximal group of land cells.

Example 1
Input: grade = [["1","1","0"],["0","1","0"],["1","0","1"]]
Output: 3
Explanation: Diagonal cells are not connected.
Example 2
Input: grade = [["1","1","1","1"]]
Output: 1
Constraints
  • 0 <= linhas, colunas <= 300
  • Each cell contains "0" or "1".
GraphsDFSMatrix
Caso 1
grade =
[["1","1","0"],["0","1","0"],["1","0","1"]]
Expected =
3
Caso 2
grade =
[["1","1","1","1"]]
Expected =
1

Run checks these 2 cases. Submit runs all 6, including hidden ones.