【问题标题】:how to uncompress a given string in java recursively?如何递归地解压缩java中的给定字符串?
【发布时间】:2011-11-27 23:53:49
【问题描述】:

这很容易使用迭代,但我必须使用递归来做到这一点。我试图记录一个字符在字符串中出现的次数、位置和字符串的其余部分以及输出。

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;
    }

【问题讨论】:

  • 有什么问题?问题是什么?标题中有问题,但正文中没有。
  • 如何解压缩给定的字符串,例如:“3b2a”---- bbbaa。我有我正在处理的代码..
  • 你为什么要递归地做这个——这是作业吗?

标签: java string recursion


【解决方案1】:

您的代码中有多个错误,例如:

  • 你是子串,但也传递一个位置,你应该做一个或另一个
  • 您的基本情况正在返回“”,但它应该返回累积的字符串“输出”
  • 在递归时忽略返回方法的输出,只返回当前方法中的输出,因此递归不会建立任何内容

下面是只使用递归来解析字符串和构建输出的代码。我添加了 cmets 来显示代码中发生的事情。请注意,特别是在递归中,打印当前状态很有用,这样您就可以看到每个阶段发生了什么,所以我也添加了这个。

请注意,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);
    }
}

【讨论】:

  • 非常好的答案,给出响应和诀窍。我希望OP能理解它,而不仅仅是复制粘贴。
  • @Vash:你为什么要删除你在dynamic-mouse-listeners 上的帖子的答案?我认为它非常有趣和有用,它教会了我一些新东西。当它消失时,我正要投票。
  • @HovercraftFullOfEels,我真的很累,这个例子没有实现 OP 描述的所有逻辑。我已取消删除它,稍后我将为其添加缺少的逻辑。
【解决方案2】:

假设输入的字符串格式正确,试试这个:

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';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2013-04-27
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多