C
C#2y ago
morry329#

✅ why is it '1' instead of '0'??

So I am analysing this solution https://leetcode.com/problems/number-of-islands/solutions/3585795/c-flood-fill-solution-beats-90-easy-to-understand-clean/?envType=study-plan&envId=level-1&plan=leetcode-75 Just one block of code puts me off very much:
if(grid[x][y] == '1'){
grid[x][y] = '0';
}
if(grid[x][y] == '1'){
grid[x][y] = '0';
}
` Why is the code like this, if the grid is painted '1' then the grid turns '0'? I tried to play around with this block for understanding it (swap '0' and '1' or set both of the chars to '1' '1' etc), my try led to stack overflow.
LeetCode
C#| Flood fill solution, beats 90%, Easy to understand, clean. - Nu...
View shayke223's solution of Number of Islands on LeetCode, the world's largest programming community.
2 Replies
Hugh
Hugh2y ago
This code looks to be identifying the number of islands of "1"s in the array. So it finds the first cell in the 2D array that is "1" and then marks all cells around it that are also "1" as "0", and then all cells around those. It continues doing this until all cells that were connected to the first one are marked "0", thereby removing this "island". It then increments the island counter, and then goes on to find another cell that is "1" and does it all again, finding a second "island"
morry329#
morry329#OP2y ago
Oooh thank you so much - now it is clear to me 🙂
Want results from more Discord servers?
Add your server