我们观察规律发现,我们每一位会跟后面的位置进行互换,这样的我们就递归就好.
public class demo {
/**
* @param args
*/
public static void main(String[] args) {
int a[] = {1,2,3,4};
int left = 0;
int right = a.length - 1;
solve(a,left,right);
}
private static void solve(int[] a, int left, int right) {
// TODO Auto-generated method stub
if(left==right){
for(int i = 0;i<a.length;i++){
System.out.print(a[i]);
}
System.out.println();
}else{
for(int i = left;i<=right;i++){
swap(a,left,i);
solve(a,left+1,right);
swap(a,left,i);
}
}
}
private static void swap(int[] a, int left, int right) {
int temp = a[left];
a[left] = a[right];
a[right] = temp;
}
}