情况是这样的:
mylist = range(100)我有一个从src=1到的不连续路径,dest=99其中的每个pre[100]值都代表elem. path问题是:我怎样才能得到from toelems中的所有内容。可以这样做:pathdestsrc
path = []
i = dst
while i != src:
path.append(i)
i = pre[i]
path.append(src)
但是,有没有更简单的方法可能只使用一个语句?
样本输入
dst, src = 3, 2
pre = [2, 0, 3, 1]
输出
[3, 1, 0, 2] #It just have to follow the path with the indices from 3 to 2.
解释:
src dst
vv
指数:0 1 2 3
上一页 : 2 0 3 1
从dest 3开始,前身是1,所以我们去1。
从节点 1 开始,前任是 0,所以我们去 0。
从节点 0 开始,前任是 2,所以我们去 2。
2 是 src,所以我们完成了。