这题一看就知道要用DFS 思路很简单,但是由于DFS方面写的不多 导致代码实现的时候遇到了困难
第一次的代码
void dfs(int index,int** returnArray,int* returnSize,int** returnColumnSizes,
int** graph, int graphSize, int* graphColSize,bool* vis)
{
vis[index]=true;
returnArray[*returnSize][(*returnColumnSizes)[*returnSize]]=index;
(*returnColumnSizes)[*returnSize]++;
if(index==graphSize-1)
{
for(int i=0;i<(*returnColumnSizes)[*returnSize];i++)
{
returnArray[*returnSize+1][i]=returnArray[*returnSize][i];
(*returnColumnSizes)[*returnSize+1]=(*returnColumnSizes)[*returnSize];
}
(*returnSize)++;
return;
}
for(int i=0;i<graphColSize[index];i++)
{
if(vis[graph[index][i]]==false)
{
dfs(graph[index][i],returnArray,returnSize,returnColumnSizes,graph,graphSize,graphColSize,vis);
vis[graph[index][i]]=false;
(*returnColumnSizes)[*returnSize]--;
}
}
}
int** allPathsSourceTarget(int** graph, int graphSize, int* graphColSize, int* returnSize, int** returnColumnSizes){
bool vis[graphSize];
memset(vis,0,sizeof(vis));
int** returnArray=(int**)malloc(16384*sizeof(int*));
for(int i=0;i<16384;i++)
{
returnArray[i]=(int*)malloc(graphSize*sizeof(int));
}
(*returnColumnSizes)=(int*)malloc(16384*sizeof(int));
memset((*returnColumnSizes),0,sizeof(*returnColumnSizes));
*returnSize=0;
dfs(0,returnArray,returnSize,returnColumnSizes,graph,graphSize,graphColSize,vis);
return returnArray;
}
时间复杂度很大,因为初始化returnArray这个二维数组的时候初始化为16384次,可用一个temp数组来存储中间的路径结果,当到达终点时,复制给returnArray数组,同时因为题目说是无环的,所以vis数组也可省略
int temp[15];
int tempSize;
void dfs(int index,int** returnArray,int* returnSize,int** returnColumnSizes,
int** graph, int graphSize, int* graphColSize)
{
temp[tempSize++]=index;
if(index==graphSize-1)
{
returnArray[(*returnSize)]=(int*)malloc(tempSize*sizeof(int));
memcpy(returnArray[(*returnSize)],temp,tempSize*sizeof(int));
(*returnColumnSizes)[(*returnSize)++]=tempSize;
return;
}
for(int i=0;i<graphColSize[index];i++)
{
dfs(graph[index][i],returnArray,returnSize,returnColumnSizes,graph,graphSize,graphColSize);
tempSize--;
}
}
int** allPathsSourceTarget(int** graph, int graphSize, int* graphColSize, int* returnSize, int** returnColumnSizes){
int** returnArray=(int**)malloc(16384*sizeof(int*));
(*returnColumnSizes)=(int*)malloc(16384*sizeof(int));
*returnSize=0;
tempSize=0;
dfs(0,returnArray,returnSize,returnColumnSizes,graph,graphSize,graphColSize);
return returnArray;
}
这个代码与上卖弄的代码相比快了很多
但是使用了全局变量
可以改成参数,减少全局变量的使用
void dfs(int index,int** returnArray,int* returnSize,int** returnColumnSizes,
int** graph, int graphSize, int* graphColSize,int* temp,int* tempSize)
{
temp[(*tempSize)++]=index;
if(index==graphSize-1)
{
returnArray[(*returnSize)]=(int*)malloc((*tempSize)*sizeof(int));
memcpy(returnArray[(*returnSize)],temp,(*tempSize)*sizeof(int));
(*returnColumnSizes)[(*returnSize)++]=*tempSize;
return;
}
for(int i=0;i<graphColSize[index];i++)
{
dfs(graph[index][i],returnArray,returnSize,returnColumnSizes,graph,graphSize,graphColSize,temp,tempSize);
(*tempSize)--;
}
}
int** allPathsSourceTarget(int** graph, int graphSize, int* graphColSize, int* returnSize, int** returnColumnSizes){
int** returnArray=(int**)malloc(16384*sizeof(int*));
(*returnColumnSizes)=(int*)malloc(16384*sizeof(int));
*returnSize=0;
int temp[15];
int tempSize=0;
dfs(0,returnArray,returnSize,returnColumnSizes,graph,graphSize,graphColSize,temp,&tempSize);
return returnArray;
}
16384