【leetcode 拓扑排序,题号207、210】

秉持努力克服 记的速度赶不上忘的速度 基本原则,决定把最近刷的leetcode总结一下。

最好写的就是拓扑排序了,毕竟只有那么点题目= =。

1. leetcode 207 Course Schedule

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?

对题目中给定的vector>& prerequisites进行分析,创建每个课程的后序可选课程数组和入度数组【例如0->1,1的前置课程为0,则数组中存放vec[0][i] = 1,inNum[1] = inNum[1] + 1】,将前置课程数组大小为0的课程添加到队列中;出队,遍历出队课程的后序可选数组并将数组中元素对应的入度减1,如果有入度为0的添加到队列中。将遍历过的课程添加到visit数组中,如果全部遍历则可以完成全部课程

【用的bfs的方法,visit数组改为队列出列的个数maybe也可以】

 

2. leetcode 210 Course Schedule II

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, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

example:
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both     
             courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. 
             So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .

和上一题的区别就在于需要给出顺序,将队列中出队的课程push_back到res中就可以啦。

 

3. leetcode 444 Sequence Reconstruction

Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence.

example1:
Input:
org: [1,2,3], seqs: [[1,2],[1,3]]

Output:
false

Explanation:
[1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.

example2:
Input:
org: [4,1,5,2,6,3], seqs: [[5,2,6,3],[4,1,5,2]]

Output:
true

example3:
Input:
org: [1,2,3], seqs: [[1,2]]

Output:
false

Explanation:
The reconstructed sequence can only be [1,2].

首先注意到 需要保证队列中的元素只有一个,否则会出现example1中非唯一的情况;同时seqs中出现的和org中的数字数目之和应该是一样的,否则会出现example3中的情况; 另外需要对出现的数字是否在0-10000内进行判断;

3.1 入度数组初始化为-1,遍历vector>& seqs,对入度数组中出现的元素赋值0;

3.2 判断入度数组中出现的数目和vector& org中的出现数目进行比较,如果不一致则返回false;

3.3 构建map[cur][last];

3.4 将入度为0的添加到队列中,注意队列中的数目应该==1。

...其余重复1 和 2,基本一致

 

4. leetcode 329 Longest Increasing Path in a Matrix【这一题感觉做过无数遍,但是看见仍然还需要一阵迷茫】

Given an integer matrix, find the length of the longest increasing path.

From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

example1:
Input: nums = 
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
] 
Output: 4 
Explanation: The longest increasing path is [1, 2, 6, 9].

【啊呀,看了代码这一题的思路好像是大佬给的,不想写了=-=】

你可能感兴趣的:(leetcode)