2015-11-01 73 views
-3

我在過去的3個小時中遇到了一個可能很簡單的問題。我重寫了一個類,並用Lists替換了2個String參數。 問題是,當調用rekursive方法時,您會向第一個字符串參數添加1個字符。當參數的長度達到7的長度時,它會將其打印出來。字符串永遠不會超過7. 我用一個整數列表替換它,因爲字符串只包含數字。 列表雖然不斷變得越來越長,我不知道爲什麼。我希望我能正確解釋一切。如果沒有,請問我。用遞歸方法的列表替換字符串參數

這個問題可能是非常容易回答你們。

這是第一堂課,工作。

package Uebung4; 

public class PermAll_Alt { 

static int counter = 0; 

private static void permutation(String word, String str) { 

    int n = str.length(); 

    // System.out.println(str + " Str"); 
    // System.out.println(word + " word"); 

    if (n == 0) { 

     if (

     (Integer.parseInt(word.substring(0, 1))) > (Integer.parseInt(word.substring(1, 2))) 
       && (Integer.parseInt(word.substring(1, 2))) < (Integer.parseInt(word.substring(2, 3))) 
       && (Integer.parseInt(word.substring(2, 3))) > (Integer.parseInt(word.substring(3, 4))) 
       && (Integer.parseInt(word.substring(3, 4))) < (Integer.parseInt(word.substring(4, 5))) 
       && (Integer.parseInt(word.substring(4, 5))) > (Integer.parseInt(word.substring(5, 6))) 
       && (Integer.parseInt(word.substring(5, 6))) < (Integer.parseInt(word.substring(6, 7))) 

     ) { 
      // System.out.println(word); 
      counter++; 
     } 

    } else { 
     for (int i = 0; i < n; i++) { 
      // System.out.println("Word: " +word+"\t str charat: 
      // "+str.charAt(i)); 
      // System.out.println(word + str.charAt(i) + " \t combined"); 

      System.out.println("substr(0,i): " + str.substring(0, i) + " substr(i+1) " + str.substring(i + 1)); 
      permutation(word + str.charAt(i), str.substring(0, i) + str.substring(i + 1)); 
     } 
    } 
} 

public static void main(String[] args) { 
    permutation("", "1234567"); 
    System.out.println("Anzahl: " + counter); 
} 
} 

這裏是我的課,我編輯:

package Uebung4; 

import java.util.ArrayList; 
import java.util.List; 

public class PermAll { 

static int counter = 0; 

private static void permutation(List<Integer> wordList, List<Integer> lis) { 

    // List<Integer> wordList2 = cloneList(wordList); 

    int n = lis.size(); 

    if (n == 0) { 
     String word = ""; 
     for (Integer i : wordList) { 
      word += i; 
     } 


     if ((Integer.parseInt(word.substring(0, 1))) > (Integer.parseInt(word.substring(1, 2))) 
       && (Integer.parseInt(word.substring(1, 2))) < (Integer.parseInt(word.substring(2, 3))) 
       && (Integer.parseInt(word.substring(2, 3))) > (Integer.parseInt(word.substring(3, 4))) 
       && (Integer.parseInt(word.substring(3, 4))) < (Integer.parseInt(word.substring(4, 5))) 
       && (Integer.parseInt(word.substring(4, 5))) > (Integer.parseInt(word.substring(5, 6))) 
       && (Integer.parseInt(word.substring(5, 6))) < (Integer.parseInt(word.substring(6, 7))) 

     ) { 
      System.out.println(word); 
      // convertToDU(word); 
      counter++; 
     } 

    } else { 

     for (int i = 0; i < n; i++) { 
      List<Integer> tempLis = new ArrayList<>(); 

      //String tempString = ""; 

      for (int j = 0; j < i; j++) { 
       tempLis.add(lis.get(j)); 
      } 
      System.out.print("str.substr(0,i): " + tempLis+"\t"); 


      for (int k = i + 1; k < lis.size(); k++) { 
       tempLis.add(lis.get(k)); 
       System.out.print(""+lis.get(k)+", "); 
      } 
      System.out.println(tempLis); 

      // System.out.println("word "+wordList + "\t charat: 
      // "+lis.get(i)); 
      wordList.add(lis.get(i)); 

      // System.out.println(wordList + " \t kombiniert"); 

      permutation(wordList, tempLis); 
      // permutation(word + lis.get(i),tempLis); 
     } 

    } 
} 

public static void main(String[] args) { 

    List<Integer> list = new ArrayList<Integer>(); 

    int anzahl = 7; 

    for (int i = 1; i <= anzahl; i++) { 
     list.add(i); 
    } 

    String para = ""; 
    for (Integer i : list) { 
     para += i; 
    } 

    List<Integer> abc = new ArrayList<>(); 

    permutation(abc, list); 
    System.out.println("Anzahl: " + counter); 

} 
} 

回答

1

這裏是一個解決方案,我拿了這是做遞歸調用的字符串ARGS版本的代碼和複製的邏輯列表args版本:

 for (int i = 0; i < n; i++) { 
      // create a copy of wordList 
      List<Integer> permWordList = new ArrayList<Integer>(wordList); 

      // equiv to "word + str.charAt(i)" 
      permWordList.add(strLis.get(i)); 

      // create a copy of lis 
      List<Integer> permStrList = new ArrayList<Integer>(lis); 

      // equiv to "str.substring(0, i) + str.substring(i + 1)" 
      permStrList.remove(i); 

      permutation(permWordList, permStrList); 
     } 
+0

似乎工作得很好。非常感謝!你能否給我一些解釋,爲什麼我必須複製列表「lis」?天才的想法,只刪除一個列表中的元素,而不是在我周圍計數。我沒有看到這些,我感到很蠢。 – Cappuccino90

+1

您需要創建wordList和lis的副本,以便每次迭代都得到新副本(否則,lis將變得越來越小) 最後,請通過接受和/或upvoting來顯示您的讚賞... –