2017-01-28 1 views
0

permuteAndPrintValuesThreeLists_Iterativeメソッドを再帰的に実行する方法が不思議です...配列のソートとバイナリ検索の基本的な再帰はわかりますが、再帰的にメソッドを作成する方法を理解できません。複数のリストを持つ再帰的順列

再帰を使用したい理由は、3つ以上のリストを追加する可能性があり、別のforループを追加してメソッドを変更する必要がないためです。

質問permuteAndPrintValuesThreeListsメソッドをrecursiveメソッドとして記述するにはどうすればよいですか?

私の出力は次のようになります。

1 1 10 10 100 100 
1 1 10 10 200 200 
1 1 10 10 300 300 
1 1 20 20 100 100 
1 1 20 20 200 200 
1 1 20 20 300 300 
2 2 10 10 100 100 
2 2 10 10 200 200 
2 2 10 10 300 300 
2 2 20 20 100 100 
2 2 20 20 200 200 
2 2 20 20 300 300 

しかし、それは次のとおりです。

1 1 10 10 100 100 
200 200 
300 300 
400 400 
20 20 100 100 
200 200 
300 300 
400 400 
3 3 10 10 100 100 
200 200 
300 300 
400 400 
20 20 100 100 
200 200 
300 300 
400 400 

final class Problem { 

    public static void main(String[] args) { 
    Problem p = new Problem(); 
    p.permuteAndPrintValuesThreeLists_Iterative(); 
    } 

    private static List<int[]> l1; 
    private static List<int[]> l2; 
    private static List<int[]> l3; 

    private Problem() { 
    l1 = new ArrayList<>(); 
    l1.add(new int[] { 1, 1 }); 
    l1.add(new int[] { 2, 2 }); 

    l2 = new ArrayList<>(); 
    l2.add(new int[] { 10, 10 }); 
    l2.add(new int[] { 20, 20 }); 

    l3 = new ArrayList<>(); 
    l3.add(new int[] { 100, 100 }); 
    l3.add(new int[] { 200, 200 }); 
    l3.add(new int[] { 300, 300 }); 
    } 

    private static void permuteAndPrintValuesThreeLists_Iterative() { 
    for (int i = 0; i < l1.size(); i++) { 
     for (int j = 0; j < l2.size(); j++) { 
     for (int k = 0; k < l3.size(); k++) { 
      printArray(l1.get(i)); 
      printArray(l2.get(j)); 
      printArray(l3.get(k)); 
      System.out.println(); 
     } 
     } 
    } 
    } 

    private static void printArray(int[] a) { 
    for (int i : a) { 
     System.out.println(i + " "); 
    } 
    } 

} 

は、これまでのところ、私は3つのリストを含むリストを持っている必要があります知っていました(中私の場合はHashMapを追加しました)。私はまた、部分的にtry catchブロックを追加することにより、問題

private static Map<Integer, List<int[]>> allLists = new HashMap<>(); 
private static void permuteAndPrintValuesThreeLists_Recursion(List<int[]> resultList, int mapIndex) { 
    if (mapIndex == allLists.size()) { 
     // Debug code 
     for (int[] arr : resultList) 
      for (int i = 0; i < arr.length; i++) 
       System.out.println(arr[i] + " "); 
     resultList.clear(); 
     System.out.println(); 
     return; 
    } 
    for (int i = 0; i < allLists.get(mapIndex).size(); i++) { 
     int[] tmpArray = allLists.get(mapIndex).get(i); 
     resultList.add(tmpArray); 
     permuteAndPrintValuesThreeLists_Recursion(resultList, mapIndex + 1); 
    } 
} 
+3

ようこそスタックオーバーフロー!私たちはQ&Aサイトであり、雇用者向けサービスではありません。これまでに何を試みたのか、それがうまくいかなかった理由を説明してください。 –

+0

これは、文字列 "abc"のすべての順列を見つける一般的な問題ではあるが、あなたに考えを与えるかもしれない。あなたの関数が 'perms'と呼ばれ、入力としての文字列と出力としての文字列のリストを取るとしましょう。再帰の基本ケースは、文字列が1文字長い場合です。 –

+0

再帰的な場合、与えられたperms(インデックス0..n-1の文字列)、perms(インデックス1.nの文字列)によって返された各置換に対して、与えられたpermsを返します。 -1)、インデックス0、インデックス1、インデックス2の順列で、文字列の最後に文字列[0]を挿入します。 'n'階乗になるこの文字列のリストを返します。 –

答えて

0

を解決し、このソリューションのメソッドを持って、私は私の質問に印刷出力を得ました。 setを使用して、置換の新しい値を追加します。

これで4番目のリストが必要な場合でも、このメソッドは引き続き使用できます。

private static void solution_withRecursion(List<int[]> resultList, int mapIndex) { 
    if (mapIndex == allLists.size()) { 
     printListValues(resultList); 
     return; 
    } 
    for (int i = 0; i < allLists.get(mapIndex).size(); i++) { 
     int[] tmpArray = allLists.get(mapIndex).get(i); 
     try { 
      resultList.set(mapIndex, tmpArray); 
     } catch (IndexOutOfBoundsException e) { 
      resultList.add(tmpArray); 
     } 
     solution_withRecursion(resultList, mapIndex + 1); 
    } 
} 


private static void printListValues(List<int[]> list) { 
    for (int[] arr : list) { 
     for (int i : arr) { 
      System.out.print(i + " "); 
     } 
    } 
    System.out.println(); 
} 
関連する問題