java 打印组合排列,输出全组合字符序列排列的算法

# re: 输出全组合字符序列排列的算法[未登录]

谢谢你的文章。

根据你的思路, 写了一个简单的算法,好像不需要上边那么复杂。这里我用true代表1, false代表0。看看有没有问题。

---------------------------------------

public class TestFastCombination {

/**

* @param args

*/

public static void main(String[] args) {

TestFastCombination comb = new TestFastCombination();

comb.combine(5);

}

private void combine(int totalSize){

//int currentSize = 0;

boolean[] myList = new boolean[totalSize];

for(int currentSize = 3; currentSize

//initial

for(int i=0;i

if(i

myList[i] = true;

}

else{

myList[i] = false;

}

}

//PrintFirst combination

for(int i = 0;i

System.out.print(myList[i]+" ");

}

System.out.println();

//start to search 10 and move left 1s to left most

findAndChange10(myList);

}

}

private void findAndChange10(boolean[] tempList){

int leftOneNumbers = 0;

int leftZeroNumbers = 0;

boolean isFirstPairMatch = false;

do{

leftOneNumbers = leftZeroNumbers = 0;

isFirstPairMatch = false;

for(int i=0;i

if(tempList[i]==true ){

leftOneNumbers++;

if(tempList[i+1]==false){

tempList[i]=false;

tempList[i+1]=true;

leftOneNumbers--;

if(i==0){

isFirstPairMatch = true;

}

break;

}

}

else{

leftZeroNumbers++;

}

}

if(leftOneNumbers>0){

for(int j=0;j

tempList[j]=true;

}

if(leftZeroNumbers>0){

for(int j=0;j

tempList[leftOneNumbers+j]=false;

}

}

}

//Here I got a other possible combination

for(int i = 0;i

System.out.print(tempList[i]+" ");

}

System.out.println();

}

while(leftOneNumbers>0 ||(isFirstPairMatch));

}

}

随风 评论于 2007-10-23 09:05  回复  更多评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值