Java实现全排列

public static void qpl(char[] a,int begin,int end){
  if(begin==end-1){
   for(int j=0;j<end;j++)
    System.out.print(a[j]);
   System.out.println();
  }
  else{
   for(int i=begin;i<end;i++){
    char tmp=a[i];
    a[i]=a[begin];
    a[begin]=tmp;
    qpl(a,begin+1,end);
    tmp=a[i];
    a[i]=a[begin];
    a[begin]=tmp;
   }
  }
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  char[] a={'1','2','3','4'};
  System.out.print("全排列为:");
  qpl(a,0,a.length);
 }
}


结果:全排列为:1234
1243
1324
1342
1432
1423
2134
2143
2314
2341
2431
2413
3214
3241
3124
3142
3412
3421
4231
4213
4321
4312
4132
4123
参考博客:http://blog.youkuaiyun.com/wuzhekai1985/article/details/6643127


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值