// test1.cpp : Defines the entry point for the console application. // #include "stdafx.h" void PrintArray(int *A, unsigned int N) { for (unsigned int i = 0; i < N; i++) { printf("%d ", A[i]); } printf("\r\n"); } // This method print out an array iteratively. // The idea behind this is: // 1. Suppose we have a way to print out permutation for A[n-1]. // 2. For every permutation, insert the nth element between any of // the n-1 elements will construct a new permuation // F
【在 d****n 的大作中提到】 : void IterativePrintPermutation2(int *A, unsigned int N) : { : int *controller = new int[N]; : for (unsigned int i = 0; i < N; i++) : controller[i] = i; : : while (1) : { : PrintArray(A, N); :
d*n
8 楼
First, not sure if this is the fatest one. I did it for fun a while ago. Hope no interviewer will ask such kind of question. Hints: Think about # of permutations for n size array is 1*2*3*...*(n-1)*n. if you have permutations for 1,2,3,...(n-1). what you need to do is to switch one of the element with the nth and repeat the permutation for length (n-1). all the elements after (n-1)th will be untouched.
【在 d****n 的大作中提到】 : First, not sure if this is the fatest one. I did it for fun a while ago. : Hope no interviewer will ask such kind of question. : Hints: : Think about # of permutations for n size array is 1*2*3*...*(n-1)*n. : if you have permutations for 1,2,3,...(n-1). what you need to do is to : switch one of the element with the nth and repeat the permutation for length : (n-1). all the elements after (n-1)th will be untouched.