字符串-放回全排列

题目:求一个字符集合的放回全排列。
如:{a,b,c},输出结果为27种:

I/System.out: aaa
I/System.out: aab
I/System.out: aac
I/System.out: aba
I/System.out: abb
I/System.out: abc
I/System.out: aca
I/System.out: acb
I/System.out: acc
I/System.out: baa
I/System.out: bab
I/System.out: bac
I/System.out: bba
I/System.out: bbb
I/System.out: bbc
I/System.out: bca
I/System.out: bcb
I/System.out: bcc
I/System.out: caa
I/System.out: cab
I/System.out: cac
I/System.out: cba
I/System.out: cbb
I/System.out: cbc
I/System.out: cca
I/System.out: ccb
I/System.out: ccc

参见:Java实现全排列、组合算法
Java代码实现为:

    /**
     * 放回全排列
     * 若干了个(char[]的长度)筛子的组合
     */
    public static char[] cs;

    public void startRePerm(char[] str) {
        cs = String.valueOf(str).toCharArray();
        rePerm(str, 0);
    }

    private void rePerm(char[] str, int pos) {
        if (pos == str.length - 1) {
            for (int i = 0; i < str.length; i++) {
                str[pos] = cs[i];
                System.out.println(str);
            }
            return;
        }
        for (int i = 0; i < str.length; i++) {
            str[pos] = cs[i];
            rePerm(str, pos + 1);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值