当前位置:网站首页>LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
2022-07-26 03:05:00 【小迅想变强】
链接:https://leetcode.cn/problems/ur2n8P/solution/by-xun-ge-v-nayf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目

示例

思路
解题思路
拓扑排序
将sequences集合看成一个有向图,题目 -> 判断有向图中是否存在唯一路径
比如sequences = [1,3][1,4][2,3] 当我们选择 1 为这个有向图的人口时,入度为0有3,和 4(就是当选择1作为图的人口时,有3,4两个方向可以选择), 那么此时就出现两条路,不是唯一,返回false即可,题目要求在有向图中遍历时始终只有一个方向,即始终保持整个有向图入度为0的元素唯一存在。
具体实现
很显然这里需要用到拓扑排序,定义数组ans初始化为0,将sequences中所有元素的入度进行统计并记录在ans中,整个图的大小为numsSize,所有ans大小也为numsSize+1,当有向图sequences中不存在nums中的一些元素时,可以理解为有向图会有多个入口,肯定会不满足要求。
代码
bool sequenceReconstruction(int* nums, int numsSize, int** sequences, int sequencesSize, int* sequencesColSize){
int ans[numsSize+1];//初始化入度
int queue[2];//记录度为0的元素,因为题目要求始终保持1个,所有不需要很大
memset(ans, 0, sizeof(int) * (numsSize+1));
for(int i = 0; i < sequencesSize; i++)//计算所有入度情况
{
for(int j = 1; j < sequencesColSize[i]; j++)
{
ans[sequences[i][j]]++;
}
}
int node = 0;
for(int i = 1; i <= numsSize; i++)//寻找度为0的元素
{
if(ans[i] == 0)
{
queue[node++] = i;
if(node == 2)//度为0的情况有两个了,不满足要求
{
return false;
}
}
}
while(node)//只要存在度为0的情况,就一直循环
{
node = 0;//重新将度为0的入队
int mm = queue[0];
for(int i = 0; i < sequencesSize; i++)
{
for(int j = 0; j < sequencesColSize[i] - 1; j++)
{
if(sequences[i][j] == mm)//将mm指向的下一个元素度减1,因为sequences[i][j]已经被处理了
{
ans[sequences[i][j+1]]--;
if(ans[sequences[i][j+1]] == 0)//度为0就入队列,等着被处理
{
queue[node++] = ans[sequences[i][j+1]];
if(node == 2)
{
return false;
}
}
}
}
}
}
return true;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/ur2n8P/solution/by-xun-ge-v-nayf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。时间空间复杂度

边栏推荐
- 手把手教你依赖管理
- pbootcms上传缩略图尺寸自动缩小变模糊
- [translation] announce Vites 13
- 事半功倍:学会WEB性能测试用例设计模型
- Safety margin of mass consumption
- Detailed explanation of extended physics informedneural networks paper
- Chapter 3 business function development (delete clues)
- Games101 review: shading, rendering pipelines
- cmd cpm 命令汇总
- GoLang 抽奖系统 设计
猜你喜欢
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

ENVI_ Idl: create HDF5 file and write data (take writing GeoTIFF file to HDF file as an example) + detailed parsing

VOFA+ 串口调试助手

AMD64 (x86_64) architecture ABI document:

Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?

复制列表时踩过的坑:浅拷贝与深拷贝

如何有效的去防止别人穿该网站首页快照

Arthas download and startup

Win11麦克风权限的开启方法

Influence of middle tap change on ZVS oscillation circuit
随机推荐
ES6 set and map
Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?
.net serialize enumeration as string
Teach you to rely on management hand in hand
(九)属性自省
[sql] usage of self connection
复制列表时踩过的坑:浅拷贝与深拷贝
C language layered understanding (C language function)
Article setting top
Skill list of image processing experts
[detailed explanation of key and difficult points of document operation]
Parallelloopbody in opencv
hello world驱动(二)-初级版
事半功倍:学会WEB性能测试用例设计模型
循环与分支(一)
Software testing post: Ali has three sides. Fortunately, he has made full preparations and has been offered
Image recognition (VII) | what is the pooling layer? What's the effect?
Be highly vigilant! Weaponization of smartphone location data on the battlefield
Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?
[translation] announce Vites 13