62,616
社区成员
发帖
与我相关
我的任务
分享
public class Algorithms {
public Algorithms() {
}
private static <T> void subCombination(List<List<T>> out,List<T> in,
List<T> temp,int index,int nloop,int nth){
if (index == nth){ //get the result
out.add(temp);
System.out.println(temp);
}
else{
for (int loop = nloop;loop < in.size();++loop){
temp.set(index,in.get(loop));
subCombination(out,in,temp,index+1,loop+1,nth); //backtracking
}
}
}
public static <T> void combination(List<List<T>> out,List<T> in,int nth){
if (nth >= in.size())
return;
List<T> temp = new ArrayList<T>(nth);
for (int i = 0;i < nth;++i)
temp.add(null);
subCombination(out,in,temp,0,0,nth);
}
public static void main (String[] args) {
List<String> ls = new ArrayList<String>();
ls.addAll(Arrays.asList("a","b","c","d","e","f","g","h"));
List<List<String>> comb = new ArrayList<List<String>>();
combination(comb,ls,7);
System.out.println(comb);
}
}
[a, b, c, d, e, f, g]
[a, b, c, d, e, f, h]
[a, b, c, d, e, g, h]
[a, b, c, d, f, g, h]
[a, b, c, e, f, g, h]
[a, b, d, e, f, g, h]
[a, c, d, e, f, g, h]
[b, c, d, e, f, g, h]
[[h, h, h, h, h, h, h], [h, h, h, h, h, h, h], [h, h, h, h, h, h, h], [h, h, h, h, h, h, h], [h, h, h, h, h, h, h], [h, h, h, h, h, h, h], [h, h, h, h, h, h, h], [h, h, h, h, h, h, h]]
private static <T> void subCombination(List<List<T>> out,List<T> in,
List<T> temp,int index,int nloop,int nth){
if (index == nth){ //get the result
out.add(temp);
System.out.println(temp);
}
else{
for (int loop = nloop;loop < in.size();++loop){
List<T> temp1=new ArrayList<T>(temp);
//我晕上面subCombination()的不对
//因为index等于temp1.size()
//搞得自己没看懂程序
temp1.add(in.get(loop));
subCombination(out,in,temp1,index+1,loop+1,nth); //backtracking
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Algorithms
{
public Algorithms()
{
}
private static <T> void subCombination(List<List<T>> out, List<T> in,
T[] temp, int index, int nloop, int nth)
{
if (index == nth)
{ // get the result
out.add(Arrays.asList(temp.clone()));
System.out.print("[");
for(int i=0;i<temp.length-1;i++)
System.out.print(temp[i]+", ");
System.out.print(temp[temp.length-1]+"]");
System.out.println();
}
else
{
for (int loop = nloop; loop < in.size(); ++loop)
{
//temp.set(index, in.get(loop));
temp[index] = in.get(loop);
subCombination(out, in, temp, index + 1, loop + 1, nth); // backtracking
}
}
}
public static <T> void combination(List<List<T>> out, List<T> in, int nth)
{
if (nth >= in.size())
{
return;
}
//List<T> temp = new ArrayList<T>(nth);
Object[] os = new Object[nth];
T[] temp = (T[])os;
// for (int i = 0; i < nth; ++i)
// temp.add(null);
subCombination(out, in, temp, 0, 0, nth);
}
public static void main(String[] args)
{
List<String> ls = new ArrayList<String>();
ls.addAll(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"));
List<List<String>> comb = new ArrayList<List<String>>();
combination(comb, ls, 7);
System.out.println(comb);
}
}
private static <T> void subCombination(List<List<T>> out,List<T> in,
List<T> temp,int index,int nloop,int nth){
if (index == nth){ //get the result
out.add(temp);
System.out.println(temp);
}
else{
for (int loop = nloop;loop < in.size();++loop){
List<T> temp1=new ArrayList<T>(temp);
//这里改了
if(index>temp1.size()){
temp1.set(index,in.get(loop));
}else{
temp1.add(in.get(loop));
}
subCombination(out,in,temp1,index+1,loop+1,nth); //backtracking
}
}
}
public static <T> void combination(List<List<T>> out,List<T> in,int nth){
if (nth >= in.size())
return;
List<T> temp = new ArrayList<T>(nth);
subCombination(out,in,temp,0,0,nth);
}
private static <T> void subCombination(List<List<T>> out,List<T> in,
List<T> temp,int index,int nloop,int nth){
if (index == nth){ //get the result
out.add(temp);
System.out.println(temp);
}
else{
for (int loop = nloop;loop < in.size();++loop){
List<T> temp1=new ArrayList<T>(temp);//。。。。。。。。。。
temp1.set(index,in.get(loop));
subCombination(out,in,temp1,index+1,loop+1,nth); //backtracking
}
}
}