# 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 回复 更多评论