代码拉取完成,页面将自动刷新
// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++
package main
import "fmt"
func canFinishCourses(numCourses int, array [][]int) bool {
graph := map[int][]int{}
for _, pre := range array {
graph[pre[0]] = append(graph[pre[0]], pre[1])
}
traced := map[int]bool{}
visited := map[int]bool{}
for i := 0; i < numCourses; i++ {
if hasCycle(i, graph, traced, visited) {
return false
}
}
return true
}
func hasCycle(course int, graph map[int][]int, traced, visited map[int]bool) bool {
if traced[course] {
return true
}
if visited[course] {
return false
}
traced[course] = true
for _, pre := range graph[course] {
if hasCycle(pre, graph, traced, visited) {
return true
}
}
traced[course] = false
visited[course] = true
return false
}
func main() {
array := [][]int{{1, 2}, {0, 2}}
res := canFinishCourses(2, array)
fmt.Println(res)
}
//$ go run interview5-2.go
//true
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。