add new solutions
This commit is contained in:
parent
f960020cb4
commit
14cd11f1c7
4 changed files with 155 additions and 0 deletions
31
solutions/2/q200/solution.go
Normal file
31
solutions/2/q200/solution.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package q200
|
||||
|
||||
const (
|
||||
VISITED byte = '.'
|
||||
LAND byte = '1'
|
||||
)
|
||||
|
||||
func traverse(grid [][]byte, x, y int) int {
|
||||
if x < 0 || y < 0 || x >= len(grid[0]) || y >= len(grid) || grid[y][x] != LAND {
|
||||
return 0
|
||||
}
|
||||
|
||||
grid[y][x] = VISITED
|
||||
traverse(grid, x+1, y)
|
||||
traverse(grid, x-1, y)
|
||||
traverse(grid, x, y+1)
|
||||
traverse(grid, x, y-1)
|
||||
return 1
|
||||
}
|
||||
|
||||
func numIslands(grid [][]byte) int {
|
||||
count := 0
|
||||
for y := range grid {
|
||||
for x := range grid[y] {
|
||||
count += traverse(grid, x, y)
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
var _ = numIslands
|
||||
Loading…
Add table
Add a link
Reference in a new issue