add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
25
solutions/5/q547/solution.go
Normal file
25
solutions/5/q547/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package q547
|
||||
|
||||
func walk(graph [][]int, i int, seen []bool) {
|
||||
for j := range graph {
|
||||
if graph[i][j] == 1 && !seen[j] {
|
||||
seen[j] = true
|
||||
walk(graph, j, seen)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func findCircleNum(isConnected [][]int) int {
|
||||
seen := make([]bool, len(isConnected))
|
||||
|
||||
provinces := 0
|
||||
for i := range len(isConnected) {
|
||||
if !seen[i] {
|
||||
walk(isConnected, i, seen)
|
||||
provinces++
|
||||
}
|
||||
}
|
||||
return provinces
|
||||
}
|
||||
|
||||
var _ = findCircleNum
|
||||
Loading…
Add table
Add a link
Reference in a new issue