Permutation of array

后端 未结 11 1039
我寻月下人不归
我寻月下人不归 2020-11-22 07:56

For example I have this array:

int a[] = new int[]{3,4,6,2,1};

I need list of all permutations such that if one is like this, {3,2,1

11条回答
  •  遇见更好的自我
    2020-11-22 08:16

    If you're using C++, you can use std::next_permutation from the header file:

    int a[] = {3,4,6,2,1};
    int size = sizeof(a)/sizeof(a[0]);
    std::sort(a, a+size);
    do {
      // print a's elements
    } while(std::next_permutation(a, a+size));
    

提交回复
热议问题