【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 ...
随机推荐
- JS keycode 事件响应
<script language="javascript"> function keyevent(){ if(event.keyCode==13) alert(&quo ...
- [译]Mongoose指南 - 验证
开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...
- 一个简单的猜大小的小游戏 python
初学python,用python写了一个简单的猜大小的小游戏 #!/usr/bin/env python #-*- coding:utf-8 -*- print "------------- ...
- ML_R Kmeans
Kmeans作为机器学习中入门级算法,涉及到计算距离算法的选择,聚类中心个数的选择.下面就简单介绍一下在R语言中是怎么解决这两个问题的. 参考Unsupervised Learning with R ...
- cpu利用率和cpu 队列
SIP的第四期结束了,因为控制策略的丰富,早先的的压力测试结果已经无法反映在高并发和高压力下SIP的运行状况,因此需要重新作压力测试.跟在测试人员后面做了快一周的压力测试,压力测试的报告也正式出炉,本 ...
- navigationcontroller手势翻页和navigationbar
一. 系统导航默认手势 #import "CBNavigationController.h" //手势返回 @interface CBNavigationController () ...
- [BZOJ3572][Hnoi2014]世界树
[BZOJ3572][Hnoi2014]世界树 试题描述 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森,在他们的信条 ...
- aspcms标签
[newslist:date style=yy-m-d] 日期格式 {aspcms:sitepath}/Templates/{aspcms:defaulttemplate} 幻灯片标签{aspcms: ...
- Oracle Database 11g Express Edition学习笔记
修改字符集 使用用户system,通过sqlplus程序连接到Oracle数据库,输入以下命令,查看字符集: SQL> select userenv('language') from dual; ...
- C#面向对象思想计算两点之间距离
题目为计算两点之间距离. 面向过程的思维方式,两点的横坐标之差,纵坐标之差,平方求和,再开跟,得到两点之间距离. using System; using System.Collections.Gene ...