题解 | #检测循环依赖#
检测循环依赖
https://www.nowcoder.com/practice/8dc02ad98553432a90affc3a0484910b
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param prerequisites int整型二维数组 * @param n int整型 * @return int整型一维数组 */ public List<int> findOrder (List<List<int>> prerequisites, int n) { // write code here if (prerequisites == null || prerequisites.Count == 0 || n == 0) return null; List<int> lsN = new List<int>(); List<int> lsNS = new List<int>(); List<int> lsNE = new List<int>(); for (int i = 0; i < prerequisites.Count; i++) { lsNS.Add(prerequisites[i][1]); lsNE.Add(prerequisites[i][0]); } List<int> lsNFind = lsNS.FindAll(r => !lsNE.Contains(r)); if (lsNFind == null || lsNFind.Count == 0) return new List<int>(); lsNS.AddRange(lsNFind); for (int k = 0; k < n; k++) { if (lsN.Contains(k)) continue; lsN.Add(k); } return lsN; } }