LeetCode Course Schedule II

原创 2015年07月09日 14:10:16

Description:

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

Solution:

Course Schedule一样,就是将拓扑的结果任意输出一个。然后发现一个bug,前面没注意到的,pre[i][0]是在pre[i][1]之后的意思……貌似方向搞反了

import java.util.*;

public class Solution {
	public int[] findOrder(int numCourses, int[][] prerequisites) {
		int n = numCourses;

		int topo[] = new int[n];
		int indegree[] = new int[n];
		int mat[][] = new int[n][n];

		for (int l = 0; l < prerequisites.length; l++) {
			int j = prerequisites[l][0];
			int i = prerequisites[l][1];
			if (mat[i][j] == 1)
				continue;
			mat[i][j] = 1;
			indegree[j]++;
		}

		int index;
		for (int l = 0; l < n; l++) {
			index = -1;
			for (int i = 0; i < n; i++)
				if (indegree[i] == 0) {
					indegree[i] = -1;
					index = i;
					break;
				}
			if (index == -1)
				return new int[0];
			topo[l] = index;
			for (int j = 0; j < n; j++)
				if (mat[index][j] == 1)
					indegree[j]--;
		}

		return topo;
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 210. Course Schedule II(课程安排)

原题网址:https://leetcode.com/problems/course-schedule-ii/ There are a total of n courses you have ...

LeetCode 210. Course Schedule II|图问题.拓扑排序

题目描述There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have p...

LeetCode 210 Course Schedule II

题目描述There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have p...

leetcode - 210. Course Schedule II

算法系列博客之拓扑排序 DFS和入度数组

LeetCode 210. Course Schedule II

ladys and gentleman,又到了令人激动的一周更新时刻,这周我们的主题是拓扑排序,关于拓扑排序请自行度娘,本来昨晚就可以更完的,然而五月天的演唱会吵到我无法思考,甚至短路了,导致昨晚写了...

Leetcode 210. Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prere...

leetcode - Course Schedule II

题目:leetcode Course Schedule II   There are a total of n courses you have to take, labeled from ...

LeetCode Algorithms 210. Course Schedule II

题目难度: Medium 原题描述: There are a total of n courses you have to take, labeled from 0 t...

LeetCode 210. Course Schedule II

210. Course Schedule IIThere are a total of n courses you have to take, labeled from 0 to n - 1.Some...

LeetCode Course Schedule II

拓扑排序的题目。之前写了一个简单的做法,使用n*n的矩阵来保存图。时间复杂度是O(n^2) 改用邻接表来描述图。同时使用一个数组来记录每个结点的入度。初始化都为零。依次读入边,将边加入邻接表并将记录结...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)