简体   繁体   English

递归解压缩使用RLE压缩的字符串

[英]Recursively decompressing a string that was compressed using RLE

I'm trying to write a method that decompresses a string that was compressed using the RLE format recursively. 我正在尝试编写一种方法,该方法以递归方式解压缩使用RLE格式压缩的字符串。 So, I want a method that converts "5a2*3h32" into "aaaaa**hhh222". 因此,我想要一种将“ 5a2 * 3h32”转换为“ aaaaa ** hhh222”的方法。

Input: 5a2*3h32 输入:5a2 * 3h32

Output: aaaaa**hhh222 --> this is correct 输出:aaaaa ** hhh222->这是正确的

Input: i24 (should be: i44) 输入:i24(应为:i44)

Output: Exception in thread "main" java.lang.NumberFormatException: For input string: "i" 输出:线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:“ i”

So basically it doesn't like when a character is not preceded by a digit. 因此,基本上,它不喜欢字符前面没有数字。 However, if the input had one lone character at the end of the string, the method works fine. 但是,如果输入在字符串末尾有一个单独的字符,则该方法可以正常工作。

I can't seem to figure this out. 我似乎无法弄清楚。 Any input would be really helpful! 任何输入都会真正有帮助!

public static String decompress(String compressedText){
    char c = ' ';
    String temp;

    if(compressedText.length() != 1){
        c = compressedText.charAt(1);
    }if(compressedText.length() == 1){
        return compressedText;       
    }else if(compressedText.charAt(0) == '0' && compressedText.length() != 2){
        return decompress(compressedText.substring(2));
    }else if(compressedText.charAt(0) == '0' && compressedText.length() == 2){
        compressedText = "\0";
        return compressedText;
    }else if(Character.isLetterOrDigit(c) == true || Character.isLetterOrDigit(c) == false){
        int i = Integer.parseInt(compressedText.substring(0,1));
        i = i-1;
        temp = Integer.toString(i);
        return c + decompress(temp.concat(compressedText.substring(1)));
    }if(Character.isLetter(c) == false){
        c = compressedText.charAt(2);
        int i = Integer.parseInt(compressedText.substring(0,2));
        i = i-1;
        temp = Integer.toString(i);
        return c + decompress(temp.concat(compressedText.substring(2)));
    }
    return compressedText;
}

As I commented the problem was with your condition, I change it a little bit and now it is working: 当我评论问题出在您的病情上时,我做了一点修改,现在可以正常工作了:

public static String decompress(String compressedText){
    char c = ' ';
    String temp;

    if(compressedText.length() != 1){
        c = compressedText.charAt(1);
    }if(compressedText.length() == 1){
        return compressedText;       
    }else if(compressedText.charAt(0) == '0' && compressedText.length() != 2){
        return decompress(compressedText.substring(2));
    }else if(compressedText.charAt(0) == '0' && compressedText.length() == 2){
        compressedText = "\0";
        return compressedText;
    }else if(Character.isDigit(compressedText.charAt(0)) == true){
        int i = Integer.parseInt(compressedText.substring(0,1));
        i = i-1;
        temp = Integer.toString(i);
        return c + decompress(temp.concat(compressedText.substring(1)));
    }
    return compressedText.charAt(0)+decompress(compressedText.substring(1));
}

checkout the online version here . 此处签出在线版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM