擅长:python、mysql、java
<p>进行排列的复杂性大约是O(n*n!),所以对于大数或列表,生成所有可能的排列都会很低效,可以用回溯来生成列表排列,我会分享一个链接可能会有帮助。
<a href="https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/" rel="nofollow noreferrer">The solution is based on the backtracking</a></p>
<p/><div^{cl1}$&13;
<div^{cl2}$&13;
<pre class="snippet-code-html lang-html prettyprint-override"><code>
def permute(a, l, r):
if l == r:
print(a)
else:
for i in range(l, r + 1):
a[l], a[i] = a[i], a[l]
permute(a, l + 1, r)
a[l], a[i] = a[i], a[l]
data = [1,2,3,4,5]
n = len(data)
a = list(data)
permute(a, 0, n - 1)</code></pre>
;
</div>和13;
</div>和13;