2011-11-27 63 views
1

這是很容易使用迭代,但我必須使用遞歸來做到這一點。我試着保留一個字符串中字符出現的次數,位置以及字符串和輸出的其餘部分。如何在java中遞歸地解壓給定的字符串?

public static String uncompress(String compressedText) { 

     return uncompress(compressedText, 1, 0, ""); 

    } 

    public static String uncompress(String text, int count, int pos, String output) { 

     if (text.equals("")) { 

      return ""; 
     } 

     if (Character.isLetter(text.charAt(pos))) { 

      output += text.charAt(0); 
      pos++; 
     } 

     else if(Character.isDigit(text.charAt(pos))) { 

      count = text.charAt(pos) - '0'; 
      output += text.charAt(pos + 1); 
      count++; 
      pos++; 

     } 

     text = text.substring(pos + 1); 

     uncompress(text, count, pos, output); 

     return output; 
    } 
+2

有什麼問題嗎?問題是什麼?標題中的問題,但沒有任何內容。 –

+0

如何解壓縮字符串,例如:「3b2a」---- bbbaa。我有我的代碼,我正在努力.. – user647207

+0

爲什麼你想這樣做遞歸 - 這是作業嗎? –

回答

2

有你的代碼中的多個錯誤,如:

  • 你substringing也傳遞一個位置,你應該做一個或另一個
  • 您的基本情況是「回」但它應該返回應計字符串的'輸出'
  • 您可以放棄不考慮返回方法的輸出並僅返回當前方法中的輸出,因此遞歸沒有任何內容建立起來

下面是僅使用遞歸來解析字符串並生成輸出的代碼。我添加了評論以顯示代碼中發生的事情。請注意,特別是在遞歸中,打印出當前狀態是很有用的,這樣您就可以看到每個階段發生了什麼,所以我也添加了這個。

請注意,getMultiple()方法本身就是遞歸應該如何工作的一個非常簡單的例子 - 您調用相同的方法,但是A)傳遞當前調用中完成的某些工作,以便可以通過基本情況或B)取得方法的輸出,並在返回修改後的輸出之前添加或修改它。

public class Recursion { 

public static void main(String[] args) { 
    System.out.println(uncompress("10a2b")); 
} 

public static String uncompress(String compressedText) { 

    return uncompress(compressedText, "", ""); 

} 

public static String getMultiple(char x, int N) { 
    if (N == 0) return ""; 

    return ""+x+getMultiple(x,N-1); 
} 

public static String uncompress(String text, String count, String output) { 
    System.out.println("----"); 
    System.out.println("TEXT:"+text); 
    System.out.println("COUNT:"+count); 
    System.out.println("OUTPUT:"+output); 


    if (text.equals("")) { 
     //base case - no text left to parse 

     return output; 
    } 

    if (Character.isLetter(text.charAt(0))) { 
     //letter case - need to take the count we have accrued, parse it into an integer and add to output 

     System.out.println(count);// * text.charAt(0); 

     output += getMultiple(text.charAt(0),Integer.parseInt(count)); 

     count = ""; 
    } 

    else if(Character.isDigit(text.charAt(0))) { 
     //digit case - need to add to the count but keep as a string because must be parsed later 

     count += (""+text.charAt(0)); 

    } 

    //parse the *remainder* of the string, one character at a time, so pass in the substring(1) 


     return uncompress(text.substring(1), count, output); 
    } 
} 
+0

非常好的答案,即給予迴應和知道如何。我希望OP能夠理解它,不僅僅是複製粘貼。 –

+0

只能希望:) – AntonyM

+0

@Vash:你爲什麼在[dynamic-mouse-listeners](http://stackoverflow.com/questions/8290340/dynamic-mouse-listeners)上刪除你對線程的回答?我認爲這很有趣,有用,它教會了我一些新的東西。當它消失時我正要對它進行投票。 –

1

假設輸入字符串是否具有正確的格式,試試這個:

public static String uncompress(String compressedText) { 
    if (compressedText.length() == 0) 
     return ""; 
    return uncompress(compressedText, charToInt(compressedText, 0), 0); 
} 

public static String uncompress(String text, int count, int pos) { 
    if (pos == text.length() || (pos == text.length()-2 && count == 0)) 
     return ""; 
    else if (count == 0) 
     return uncompress(text, charToInt(text, pos+2), pos+2); 
    return text.charAt(pos+1) + uncompress(text, count-1, pos); 
} 

public static int charToInt(String str, int idx) { 
    return str.charAt(idx) - '0'; 
}