字符串的组合



/**
* 题目:输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入
* abc,它的组合有a、b、c、ab、ac、bc、abc。
*/

public class Launch
{
public static void main(String ...args)
{
combination("abcd");
}

public static void combination(String str)
{
int strLength;

strLength = str.length();
tmpArray = new char[strLength];

for(int len = 1; len <= strLength; ++len)
{
tlen = len;
getCombination(str, 0, len);
}
}

private static void getCombination(String str, int index, int n)
{
int strLength = str.length();

if(n == 0)
{
for(int i = tlen - 1; i >= 0; --i)
{
System.out.print(tmpArray[i]);
}
System.out.print(" ");
}
else
{
for(int i = index; i < strLength; ++i)
{
if(strLength - index < n)
break ;

tmpArray[n - 1] = str.charAt(i);
getCombination(str, i + 1, n - 1);
}
}
}

private static int tlen;
private static char tmpArray[];
}

运行结果: a b c d ab ac ad bc bd cd abc abd acd bcd abcd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值