LeetCode Course Schedule

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

Description:

here 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, is it possible for you to finish all courses?

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 it is possible.

2, [[1,0],[0,1]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

Solution:

拓扑排序,先根据前置要求将图构建出来,同时记录每个点的入度。

进行n次循环,每次循环做下面的处理:

1. 先找入度为1的点,如果找不到,就返回false

2. 找到某一个入度为0的点,将所有从这个点能出发到的边遍历,每条边的另一个点入度都减去1

3. 如此循环n次

import java.util.*;

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

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

		for (int l = 0; l < prerequisites.length; l++) {
			int i = prerequisites[l][0];
			int j = 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 false;
			for (int j = 0; j < n; j++)
				if (mat[index][j] == 1)
					indegree[j]--;
		}

		return true;
	}
}


【LeetCode】207. Course Schedule

题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have pre...
  • u011613729
  • u011613729
  • 2016年05月30日 23:40
  • 672

[leetcode-207]Course Schedule(java)

问题描述: There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may hav...
  • zdavb
  • zdavb
  • 2015年08月26日 15:23
  • 459

leetcode 207: Course Schedule

leetcode 207 : Course Schedule
  • xudli
  • xudli
  • 2015年05月20日 05:07
  • 4319

【LeetCode】Course Schedule 解题报告

【题目】 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...
  • ljiabin
  • ljiabin
  • 2015年05月19日 17:05
  • 15021

【LeetCode】Course Schedule II 解题报告

【题目】 There are a total of n courses you have to take, labeled from 0 to n - 1. Some course...
  • ljiabin
  • ljiabin
  • 2015年05月19日 17:13
  • 8308

leetcode 210: Course Schedule II

leetcode 210: Course Schedule II Course Schedule 2 java c++ python
  • xudli
  • xudli
  • 2015年05月22日 07:15
  • 3423

LeetCode 207 - Course Schedule

一、问题描述Description:There are a total of n courses you have to take, labeled from 00 to n−1n-1.Some co...
  • lisong694767315
  • lisong694767315
  • 2015年05月07日 02:24
  • 2758

Leetcode 207 Course Schedule 课程表

你需要上n个课程,标记为0~n-1。有些课程有前导课,比如你要上课程0必须先上课程1,我们用一个整数对[0,1]来表示。给出课程的总数和一系列的课程先导关系对,判断是否可能上完所有的课程。...
  • smile_watermelon
  • smile_watermelon
  • 2015年08月10日 18:05
  • 2809

[LeetCode]Course Schedule,解题报告

题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have pre...
  • zinss26914
  • zinss26914
  • 2015年05月11日 15:37
  • 4267

Leetcode 210 Course Schedule II 课程表II

你需要上n个课程,标记为0~n-1。有些课程有前导课,比如你要上课程0必须先上课程1,我们用一个整数对[0,1]来表示。给出课程的总数和一系列的课程先导关系对,返回可以上完所有的课程的上课顺序。可能存...
  • smile_watermelon
  • smile_watermelon
  • 2015年08月14日 16:29
  • 748
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Course Schedule
举报原因:
原因补充:

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