Skip to content

Commit d9497a9

Browse files
committed
提交210 课程表II
1 parent 1dbdb5e commit d9497a9

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

210.课程表II/solution.html

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<script>
11+
// Source : https://leetcode.com/problems/course-schedule-ii/
12+
// Author : 悬笔e绝
13+
// Date : 2019-11-13
14+
15+
/**
16+
* @param {number} numCourses
17+
* @param {number[][]} prerequisites
18+
* @return {number[]}
19+
*/
20+
var findOrder = function(numCourses, prerequisites) {
21+
var map = []; // 邻接表
22+
var indegree = []; // 入度
23+
24+
for (var i = 0; i < numCourses; i++)
25+
map[i] = [], indegree[i] = 0;
26+
27+
prerequisites.forEach(function(item) {
28+
var from = item[1];
29+
var to = item[0];
30+
31+
map[from].push(to);
32+
indegree[to]++;
33+
});
34+
35+
var q = [];
36+
var finishNum = 0;
37+
var ans = [];
38+
39+
for (var i = 0; i < numCourses; i++) {
40+
// 入度为 0,没有依赖
41+
if (!indegree[i]) {
42+
q.push(i);
43+
finishNum++;
44+
}
45+
}
46+
47+
while (q.length) {
48+
var from = q.shift();
49+
ans.push(from);
50+
51+
map[from].forEach(function(to) {
52+
if (--indegree[to] === 0) {
53+
q.push(to);
54+
finishNum++;
55+
}
56+
});
57+
}
58+
59+
return finishNum === numCourses ? ans : [];
60+
};
61+
</script>
62+
</body>
63+
</html>

0 commit comments

Comments
 (0)