组合来自n的所有案例k

时间:2014-04-04 09:59:19

标签: c# java combinations

我在C#中实现了一个递归算法,它列出了来自K的N的所有可能组合,它非常好,结果非常可靠,现在我需要在java中实现它,我做了,但结果却丢失了?很多情况都没有列出,试图不止一次看到问题出在哪里,我希望你试着看看我的问题。谢谢

C#代码:

static List<string> Combinations(List<string> motList, int Longeur)
{
    List<String> Resultat = new List<string>();
    for (int i = 0; i < motList.Count; i++)
    {
        if (Longeur == 1)
            Resultat.Add(motList[i]);
        else
        {
            List<string> ListIntermediaire = motList.GetRange(i + 1, motList.Count - (i + 1));
            List<string> CombiList = Combinations(ListIntermediaire, Longeur - 1);
            foreach (string s in CombiList)
                Resultat.Add(motList[i] + s);
        }
    }
    return Resultat;
}

Java代码:

 //function to get sub arrayList (getRange in c#)
    public static ArrayList subArrayList (ArrayList ls , int i , int j)
    {
            ArrayList res = new ArrayList ();
            for (int k = i; k <= j ; k++) {
                res.add(ls.get(k));
            }
            return  res;
     }

     //Java version of combination function 
     public static ArrayList Combinaison (ArrayList motList, int Longeur)
     {
            ArrayList Resultat = new ArrayList();
            for (int i = 0; i <motList.size() ; i++) {      
                if (1 == Longeur )
                    Resultat.add(motList.get(i));
                else
                {  
                    ArrayList ListIntermediaire  = subArrayList(motList,i+1 , motList.size()-(i+1));  
                    ArrayList CombiList = Combinaison(ListIntermediaire, Longeur-1);
                    for (int j = 0; j < CombiList.size(); j++)
                          Resultat.add( motList.get(i) +""+ CombiList.get(j) );                    
                }   
            }  
            return  Resultat;
       }

2 个答案:

答案 0 :(得分:0)

您的代码并不太符合Java编码标准,如果有任何帮助,我会将其他解决方案混淆:

public static void main(final String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");
    List<String> result = getCombinations(list, 3);
    System.out.println(result);
}

private static List<String> getCombinations(final List<String> list, final int length) {
    List<String> ret = new ArrayList<>();
    if (length >= 1) {
        for (int j = 0; j < list.size(); j++) {
            List<String> tmp = new ArrayList<>(list);
            String excluded = tmp.remove(j);
            List<String> remainingCombinations = getCombinations(tmp, length - 1);
            if (remainingCombinations.size() > 0) {
                for (String s : remainingCombinations) {
                    String combination = excluded + s;
                    ret.add(combination);
                }
            } else {
                ret.add(excluded);
            }
        }
    }
    return ret;
}

答案 1 :(得分:0)

好的,我明白了你的观点。组合(而不是排列)的解决方案:

public static void main(final String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");
    List<String> result = getCombinations(list, 2);
    System.out.println(result.size());
    System.out.println(result);
}

private static List<String> getCombinations(final List<String> list, final int length) {
    if (length >= 1) {
        return removeUntilLength(list, length, 0);
    }
    return new ArrayList<>();
}

private static List<String> removeUntilLength(final List<String> list, final int length, final int lastIdx) {
    List<String> ret = new ArrayList<>();
    if (list.size() == length) {
        ret.add(list.toString());
    } else {
        for (int i = lastIdx; i < list.size(); i++) {
            List<String> tmp = new ArrayList<>(list);
            tmp.remove(i);
            ret.addAll(removeUntilLength(tmp, length, Math.max(i, 0)));
        }
    }
    return ret;
}