输入一个字符串,打印出该字符串中字符的所有排列

题目:

 输入一个字符串,打印出该字符串中字符的所有排列

 

解答:

 1 public class Solution {
 2     public static void main(String[] args) {
 3 
 4         //Scanner sc = new Scanner(System.in);
 5         //String str = sc.nextLine();
 6 
 7         myPrint("abc");
 8     }
 9 
10     private static void myPrint(String str) {
11         if(str == null) {
12             return;
13         }
14 
15         char[] chs = str.toCharArray();
16         myPrint(chs, 0);
17     }
18 
19     private static void myPrint(char[] str, int i) {
20         if(i >= str.length) {
21             return;
22         }
23 
24         if(i == str.length-1) {
25             System.out.println(String.valueOf(str));
26         } else {
27             for(int j = i; j < str.length; j++) {
28                 char temp = str[j];
29                 str[j] = str[i];
30                 str[i] = temp;
31 
32                 myPrint(str, i+1);
33 
34                 temp = str[j];
35                 str[j] = str[i];
36                 str[i] = temp;
37             }
38         }
39     }
40 }

 

转载于:https://www.cnblogs.com/wylwyl/p/10370842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值