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

Example 1:

Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
  To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: 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.

Note:

    1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
    2. You may assume that there are no duplicate edges in the input prerequisites.
  • 解题思路

这道题目是检测图内是否有环的应用题目,解决方案是经典的拓扑排序即可。之前实习面试的时候,太紧张没能想到拓扑排序。

所以,这次也特意选择拓扑排序类的题目做一下。感觉确实是不熟悉,简单的程序也调了挺久。

解这道题的基本思路如下:

    • 针对每个节点,记录依赖它的节点,以及自身依赖节点的数目。
    • 选出依赖节点数目为0的节点,移除,更新依赖它的节点的依赖节点数目。
    • 循环上述步骤,直至无点可移。
    • 判断剩余点数。

结合这个思路,编码实现如下:

class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
//initiate outNodes and inEdges
vector<int> outNodes(numCourses);
vector<vector<int>> inEdges(numCourses);
for(int i = ; i < numCourses; i++)
{
outNodes[i] = ;
}
int n = prerequisites.size();
for(int i = ; i < n; i++)
{
outNodes[prerequisites[i].first] += ;
inEdges[prerequisites[i].second].push_back(prerequisites[i].first);
} int leftNode = numCourses;
bool toFind = true;
while(toFind)
{
// find next node to move
toFind = false;
for(int i = ; i < numCourses; i++)
{
// remove the outNodes and update outNodes vector
if(outNodes[i] == )
{
outNodes[i] -= ;
for(int j = ; j < inEdges[i].size(); j++)
{
outNodes[inEdges[i][j]] -= ;
} toFind = true;
leftNode -= ;
break;
}
}
} return (leftNode == );
}
};

附加知识:pair的基本使用。

pair<int, int> p(1,2);

pair<int, int> p; p = make_pair(1,2);

p.first = p.second;

最新文章

  1. 窥探Vue.js 2.0 - Virtual&#160;DOM到底是个什么鬼?
  2. MySql 里的IFNULL、NULLIF和ISNULL用法
  3. SQL server 学习笔记1
  4. Oracle 数据类型映射C#
  5. javascript遍历控件(实例详解)
  6. 我的C# - Web - DAL- DBHelper.cs
  7. geoip 添加一列,add_field =&gt;[&quot;[geoip][request_time]&quot;,&quot;%{request_time}&quot;]
  8. 耍一把codegen,这样算懂编译么?
  9. hdu_4463(最小生成树)
  10. 最小化webpack项目
  11. 简单易懂的程序语言入门小册子(5):基于文本替换的解释器,递归,不动点,fix表达式,letrec表达式
  12. securecrt通过ssh连接板子: 密钥交换失败,没有兼容的加密程序
  13. mysql 让id字段 以1000 形式开头
  14. ss客户端以及tcp,udp,dns代理ss-tproxy本地安装版--centos7.3 x64以上(7.3-7.6x64测试通过)
  15. windows下caffe GPU版本配置
  16. 转:C# 小数位数保留的方法集锦
  17. [android] WebView与Js交互
  18. 转:从框架看PHP的五种境界及各自的薪资待遇(仅限于二三线城市,一线除外)
  19. Java中JNI的使用详解第三篇:JNIEnv类型中方法的使用
  20. OKR.2019

热门文章

  1. LARAVEL学习--安装
  2. ASP.NET全局异常处理
  3. YCRefreshView-自定义支持上拉加载更多,下拉刷新。。。
  4. 跨平台移动开发_PhoneGap 警告,通知,鸣叫,振动4 种通知类型
  5. linux c开发: 在程序退出时进行处理
  6. IT小小鸟读书笔记2
  7. 使用 yield生成迭代对象函数
  8. Linux --Mysql数据库搭建
  9. Spring中&lt;context:annotation-config/&gt;的作用
  10. css3 兼容性