我尝试使用python中的递归来生成所有可能的数组,其值为1-9。我的代码如下:
totalArr = []
def recursion(arr, n):
for i in range(9):
if (arr[i] == 0):
arr[i] = n
if (n < 8):
recursion(arr, n + 1)
else:
print(arr)
totalArr.append(arr)
recursion([0, 0, 0, 0, 0, 0, 0, 0, 0], 0)
print(len(totalArr))
当我运行这段代码时,我得到的只是下面的一个数组:
^{pr2}$我知道我可以对数组使用置换,但是对于这些数组的用例,我相信从长远来看递归更好。在
我们使用带回溯的递归进行排列: 有关https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/的详细信息
该方法可以使用分治算法生成所有排列
相关问题 更多 >
编程相关推荐