[英]Can this be done with recursion only?
I am stuck on this CodingBat recursion problem:我被困在这个CodingBat 递归问题上:
Given a string, return recursively a "cleaned" string where adjacent chars that are the same have been reduced to a single char.
给定一个字符串,递归地返回一个“清理过的”字符串,其中相同的相邻字符已减少为单个字符。 So "yyzzza" yields "yza".
所以“yyzzza”产生“yza”。
stringClean("yyzzza") → "yza"
stringClean("abbbcdd") → "abcd"
stringClean("Hello") → "Helo"
I can solve it using loops, but this is not allowed since the problem is supposed so be solved using recursion.我可以使用循环来解决它,但这是不允许的,因为问题应该使用递归来解决。 Is there any way to solve this problem without using a loop and using only recursion?
有什么办法可以不使用循环,只使用递归来解决这个问题吗? No global variables, no loops.
没有全局变量,没有循环。 I even thought of encoding some information in the parameter but that would be cheating too I think.
我什至想过在参数中编码一些信息,但我认为那也是作弊。
My previous program had no while loop, and I could only get half of the answers right.我之前的程序没有while循环,只能答对一半。 Basically, when I called my function with the string parameter, I checked the first 2 characters.
基本上,当我用字符串参数调用我的函数时,我检查了前 2 个字符。 If they were the same, I would return the character and call the function again with a string two characters smaller.
如果它们相同,我将返回该字符并用一个小两个字符的字符串再次调用该函数。 A string of 3 or 4 of the same consecutive characters would always defeat my algorithm however.
然而,由 3 或 4 个相同的连续字符组成的字符串总是会打败我的算法。
public String stringClean(String str) {
if (str.length() == 0)
return "";
if (str.length() > 1) {
int counter = 1;
char a = str.charAt(0);
char b = str.charAt(1);
if (a == b)
{
while (str.length() > 1)
{
a = str.charAt(0);
b = str.charAt(1);
if (a != b) break;
counter++;
str = str.substring(1);
}
return a + stringClean( str.substring(1) ) ;
}
}
return str.charAt(0) + stringClean (str.substring(1) );
}
My question is the following, is there any way to solve this problem without using a loop and using only recursion.
我的问题如下,有没有办法不使用循环而只使用递归来解决这个问题。 No global variables, no loops.
没有全局变量,没有循环。
Answer: Yes.答:是的。 It is very simple.
这很简单。 Try below:
尝试以下:
public String stringClean(String str) {
if (str.length() == 0)
return "";
if (str.length() == 1)
return str;
if(str.charAt(0) == str.charAt(1)){
return stringClean(str.substring(1));
}else{
return str.charAt(0)+ stringClean(str.substring(1));
}
}
Your CodingBat results in below:您的 CodingBat 结果如下:
stringClean("yyzzza") → "yza" "yza" OK
stringClean("yyzzza") → "yza" "yza" OK
stringClean("abbbcdd") → "abcd" "abcd" OKstringClean("abbbcdd") → "abcd" "abcd" OK
stringClean("Hello") → "Helo" "Helo" OKstringClean("Hello") → "Helo" "Helo" OK
stringClean("XXabcYY") → "XabcY" "XabcY" OKstringClean("XXabcYY") → "XabcY" "XabcY" OK
stringClean("112ab445") → "12ab45" "12ab45" OKstringClean("112ab445") → "12ab45" "12ab45" OK
stringClean("Hello Bookkeeper") → "Helo Bokeper" "Helo Bokeper" OKstringClean("Hello Bookkeeper") → "Helo Bokeper" "Helo Bokeper" OK
other tests OK其他测试正常
My question is the following, is there any way to solve this problem without using a loop and using only recursion.
我的问题如下,有没有办法不使用循环而只使用递归来解决这个问题。 No global variables, no loops.
没有全局变量,没有循环。
The answer is "yes it is possible".答案是“是的,这是可能的”。
Hints:提示:
public String stringClean(String str) {
if (str == null) {
return null;
} else if (str.length() > 1) {
String k = str.substring(0, 1);
if (str.charAt(0) == str.charAt(1)) {
String tmp = stringClean(str.substring(2));
return k + stringClean(tmp);
} else {
return k + stringClean(stringClean(str.substring(1)));
}
} else {
return str;
}
}
Here is my answer这是我的答案
public String stringClean(String str) {
if(str.isEmpty()) return "";
if(str.length()==1)return str;
if(str.length() > 1 && !str.substring(0,1).equals(str.substring(1,2)))
return str.substring(0,1) + stringClean(str.substring(1));
return ""+stringClean(str.substring(1));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.