watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenh5ODYxMTE0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" style="width:585px; height:438px">
代码例如以下:
package com.huan; import java.util.Arrays; public class Permutation { public static void main(String[] args) { final int SIZE = 3; int[] data = new int[SIZE]; for (int i = 1; i <= SIZE; i++){ data[i-1] = i; } recursive(data, SIZE-1); } public static void recursive(int[] data,int index){ if(index == 0){ System.out.println(Arrays.toString(data)); } for (int i = index; i >= 0 ; i--){ swap(data, index, i); recursive(data, index-1); swap(data, index, i); } } public static void swap(int[] data, int i, int j){ int temp = data[i]; data[i] = data[j]; data[j] = temp; } }
时间: 2024-11-13 04:16:17