剑指 Offer II 113. 课程顺序

拓扑排序 bfs

func findOrder(_ numCourses: Int, _ prerequisites: [[Int]]) -> [Int] {
        
        //开始构造图 edges 存储对应节点相邻的节点
        var edges = [[Int]](repeating: [], count: numCourses)
        //存储对应节点的入度
        var indeg = Array.init(repeating: 0, count: numCourses)
        for info in prerequisites {
            edges[info[1]].append(info[0])
            indeg[info[0]] += 1
        }
        var tempArray = [Int]()
        for i in 0..

dfs



func findOrder(_ numCourses: Int, _ prerequisites: [[Int]]) -> [Int] {
        
        //开始构造图
        var edges :Array> = Array.init(repeating: [], count: numCourses)
        var visited = Array.init(repeating: 0, count: numCourses)
        var hasCircle = false
        for info in prerequisites {
            edges[info[1]].append(info[0])
        }
        var res = [Int]()
        for i in 0..

你可能感兴趣的:(剑指 Offer II 113. 课程顺序)