How would you calculate all possible permutations of 0 through N iteratively?

后端 未结 10 2633
一向
一向 2020-12-04 15:47

I need to calculate permutations iteratively. The method signature looks like:

int[][] permute(int n)

For n = 3 for example, the r

10条回答
  •  醉话见心
    2020-12-04 16:23

    Is using 1.9's Array#permutation an option?

    >> a = [0,1,2].permutation(3).to_a
    => [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
    

提交回复
热议问题