【leetcode】Course Schedule(middle)☆
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?
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.
思路:
把课程序号做顶点,把给定的对作为边,就是找图里有没有环。
我自己代码:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
bool hasCircle = false;
vector<vector<int>> edges(numCourses); //换一种表示图的方式 edges[0]表示顶点0对应的边 后面是所有它指向的顶点
for(int i = ; i < prerequisites.size(); ++i)
edges[prerequisites[i].first].push_back(prerequisites[i].second);
bool * isusedv = (bool *)calloc(numCourses, sizeof(bool)); //存储顶点是否使用过
for(int i = ; i < prerequisites.size(); ++i)
{
hasCircle = findCircle(edges, isusedv, prerequisites[i].first);
if(hasCircle) break;
}
free(isusedv);
return !hasCircle;
}
bool findCircle(vector<vector<int>> &edges, bool * isusedv, int vid) //DFS
{
if(isusedv[vid])
return true; //找到了圈
isusedv[vid] = true; //标记该节点为用过
bool hasCircle = false;
for(int i = ; i < edges[vid].size(); ++i)
{
hasCircle |= findCircle(edges, isusedv, edges[vid][i]);
if(hasCircle) break; //一旦找到了圈就返回
}
isusedv[vid] = false;
return hasCircle;
}
大神的代码:
BFS拓扑排序:
一个简单的求拓扑排序的算法:首先要找到任意入度为0的一个顶点,删除它及所有相邻的边,再找入度为0的顶点,以此类推,直到删除所有顶点。顶点的删除顺序即为拓扑排序。
bool canFinish(int numCourses, vector<vector<int>>& prerequisites)
{
vector<unordered_set<int>> matrix(numCourses); // save this directed graph
for(int i = ; i < prerequisites.size(); ++ i)
matrix[prerequisites[i][]].insert(prerequisites[i][]); vector<int> d(numCourses, ); // in-degree
for(int i = ; i < numCourses; ++ i)
for(auto it = matrix[i].begin(); it != matrix[i].end(); ++ it)
++ d[*it]; for(int j = , i; j < numCourses; ++ j)
{
for(i = ; i < numCourses && d[i] != ; ++ i); // find a node whose in-degree is 0 if(i == numCourses) // if not find
return false; d[i] = -;
for(auto it = matrix[i].begin(); it != matrix[i].end(); ++ it)
-- d[*it];
} return true;
}
DFS找环
bool canFinish(int numCourses, vector<vector<int>>& prerequisites)
{
vector<unordered_set<int>> matrix(numCourses); // save this directed graph
for(int i = ; i < prerequisites.size(); ++ i)
matrix[prerequisites[i][]].insert(prerequisites[i][]); unordered_set<int> visited;
vector<bool> flag(numCourses, false);
for(int i = ; i < numCourses; ++ i)
if(!flag[i])
if(DFS(matrix, visited, i, flag))
return false;
return true;
}
bool DFS(vector<unordered_set<int>> &matrix, unordered_set<int> &visited, int b, vector<bool> &flag)
{
flag[b] = true;
visited.insert(b);
for(auto it = matrix[b].begin(); it != matrix[b].end(); ++ it)
if(visited.find(*it) != visited.end() || DFS(matrix, visited, *it, flag))
return true;
visited.erase(b);
return false;
}
【leetcode】Course Schedule(middle)☆的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- mobile touch事件
touch.js 众所周知,mobile与pc 前端开发的不同中,有一点就是事件的不同,mobile上有touchstart,touchmove,touchend等,而pc上用最多的应该还是我们的cl ...
- mysql 总结一
mysql 总结一 数据类型(四类): 整型(5种:tinyint,smallint,mediumint, int(或integer),bigint ): 浮点型:(float,double), 日期 ...
- java之远程接口调用
一.通过地址栏传值 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- zabbix 3.0.4 监控windows 服务
下载客户端 http://www.zabbix.com/download.php http://www.zabbix.com/downloads/3.0.4/zabbix_agents_3.0.4.w ...
- Lvs原理
官方文档: http://www.linuxvirtualserver.org/zh/lvs1.html http://www.linuxvirtualserver.org/zh/lvs2.html ...
- imageable.touch
使用 callback 确保创建,更新和删除 Picture 时,touch 关联的 imageable,使得其缓存能正确过期 这个update的方法用来把update时间强制更新成当前时间 http ...
- C#构造方法重载
1.什么是构造方法? 首先,它是一个方法,它是类中 众多方法中的一个.其次,它具有类中其他方法所不具备的一些特性. 在类执行开始的时候,执行这个方法. 2.构造方法相对其他方法有哪些不同? 方法名:类 ...
- 小白死去活来的安装ros_qtc_plugin
在距离写上一篇有关ROS的文章已经过去了很久了.在这段时间内一直在积累,盼望着能够厚积薄发,但还是被无情的社会折磨的死去活来,深深的体会到了一般学校和重点学校找工作的差别,以及用人单位的区别对待.说到 ...
- JavaScript深入浅出3-语句
慕课网教程视频地址:Javascript深入浅出 程序由语句组成,语句遵守特定语法规则 块 block {} 没有块级作用域 声明 var 异常 try catch finally 函 ...
- ssh(安全外壳层)
SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会 ...