有效地产生的一个链表所有可能排列? [英] Efficiently generating all possible permutations of a linked list?

查看:136
本文介绍了有效地产生的一个链表所有可能排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多算法,用于产生一组给定值的所有可能的排列。 psented作为数组通常情况下,那些价值被重新$ P $,其中有O(1)随机访问。

There are many algorithms for generating all possible permutations of a given set of values. Typically, those values are represented as an array, which has O(1) random access.

假设,psented为双向链表然而,重排的元素被重新$ P $。在这种情况下,你不能随机访问元素的列表中O(1)时间,所以很多置换算法会遇到了不必要的经济放缓。

Suppose, however, that the elements to permute are represented as a doubly-linked list. In this case, you cannot randomly access elements in the list in O(1) time, so many permutation algorithms will experience an unnecessary slowdown.

是否有一个算法,用于产生的链接列表的所有可能的排列用尽可能少的时间和空间上的开销尽可能?

Is there an algorithm for generating all possible permutations of a linked list with as little time and space overhead as possible?

推荐答案

试着想如何生成一张纸上所有排列。

Try to think of how you generate all permutations on a piece of paper.

您从最右边的数字键,然后转到一个位置到左边,直到你看到一个数字,这是比它的邻居小。比你把在那里的数字,这是下一个价值,为了所有剩余的号码后,按升序排列。这样做,直到没有什么更多的事情要做。放一点点的思想在里面,你可以订购的线性时间的数量相对于它们的数量。

You start from the rightmost number and go one position to the left until you see a number that is smaller than its neighbour. Than you place there the number that is next in value, and order all the remaining numbers in increasing order after it. Do this until there is nothing more to do. Put a little thought in it and you can order the numbers in linear time with respect to their number.

这其实是因为据我所知,用于下一置换典型的算法。我看不出有任何理由,这将是更快的数组比列表中。

This in fact is the typical algorithm used for next permutation as far as I know. I see no reason why this would be faster on array than on list.

这篇关于有效地产生的一个链表所有可能排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆