【发布时间】: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。我有我正在处理的代码..
-
你为什么要递归地做这个——这是作业吗?