Java实现全排序

package org.example.test;

import java.util.Stack;

public class QuanPaiXu {

  public static void main(String[] args) {
    perm(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, new Stack<Integer>());
  }

  public static void perm(int[] array, Stack<Integer> stack) {
    if (array.length == 0) {
      System.out.println(stack);
    } else {
      for (int i = 0; i < array.length; i++) {
        int[] tempArray = new int[array.length - 1];
        System.arraycopy(array, 0, tempArray, 0, i);
        System.arraycopy(array, i + 1, tempArray, i, array.length - i - 1);
        stack.push(array[i]);
        perm(tempArray, stack);
        stack.pop();
      }
    }
  }

}

原始链接:
https://blog.csdn.net/weixin_42220532/article/details/90900815

posted @ 2020-04-29 14:10  梦见舟  阅读(155)  评论(0编辑  收藏  举报