Permutation of array

后端 未结 11 1182
我寻月下人不归
我寻月下人不归 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:17

    Visual representation of the 3-item recursive solution: http://www.docdroid.net/ea0s/generatepermutations.pdf.html

    Breakdown:

    1. For a two-item array, there are two permutations:
      • The original array, and
      • The two elements swapped
    2. For a three-item array, there are six permutations:
      • The permutations of the bottom two elements, then
      • Swap 1st and 2nd items, and the permutations of the bottom two element
      • Swap 1st and 3rd items, and the permutations of the bottom two elements.
      • Essentially, each of the items gets its chance at the first slot

提交回复
热议问题