我正在编写来自codingbat.com的递归问题,该问题表明
Given a string, compute recursively a new string where all the lowercase 'x' chars have been moved to the end of the string.
它传递了所有示例,除了“其他测试”的示例。由于我无法看到“其他测试”指的是什么,我被卡住了。任何帮助,将不胜感激。这是我的代码
public String endX(String str) {
return endX2(str, 0, str.length());
}
public String endX2(String str, int n, int len){
if(len == 0) return str;
if(n == len-1) return str;
if(str.substring(n,n+1).equals("x")){
return str.substring(0,n) + (endX2(str.substring(n+1) + "x", n, len-1));
}
else return endX2(str, n+1, len);
}
答案 0 :(得分:1)
我不确定为什么你有一个额外的方法,因为它是不必要的。您还需要额外检查 - if(n == len-1) return str;
- 这是不需要的。
您遇到的问题是您使用未经检查的索引,并且当使用的字符串不以' x'或许多' x。如果我针对String xs = "xfooxbarxx";
使用您的代码,请为我java.lang.StringIndexOutOfBoundsException
。我还没有广泛地调试代码,但是这应该能够理解为什么它在"其他"上失败了。试验。看看我的版本,自己调查问题所在,以及如何让你的代码更简洁。
public String endX(String str) {
if(str.length() == 0)
return "";
return str.startsWith("x") ?
endX(str.substring(1)) + "x" :
String.valueOf(str.charAt(0)) + endX(str.substring(1));
}
答案 1 :(得分:0)
PS:这是比它需要更长的方式。
/*
Alright here is what we need to do...
Step 1: Get all the 'x' chars into a String.
Step 2: Get all NON 'x' chars into a String.
Step 3 (goal): Concencate (combine) the NON 'x' String first then the 'x' String in that order.
Solution Notes: Instead of using an index variable to go through a String, we could 'substring' off the first char in the String each time, cutting until we are down to the base case, for the sake of showing recursive logic I used an index varible. However on Arrays or any other collection, you need an index varible to access that element ant that spot or (index).
*/
public String endX(String str) {
//Ternary operator used here...could be written as a full if then else statement.
//Ternary operator logic: if the length is 0 return "", else return getNonX(str, "", 0) + getX(str, "", 0);
return (str.length() == 0) ? "" : getNonX(str, "", 0) + getX(str, "", 0);
//NOTICE in the parts [getNonX(str, "", 0)] and [getX(str, "", 0)]
//there is an empty String in the middle, that is there to hold or gather the
//chars, 'x' or not. We fill those empty Strings up in each recursive helper
}
public String getX(String str, String x, int index) {
//We are at the end, and if the last char IS an 'x'...
if(index == str.length() - 1 && str.charAt(index) == 'x'){
return x + 'x'; //...put that last 'x' on the 'x' String.
}
//We are at the end and if the last char IS NOT an 'x'...
else if(index == str.length() - 1 && str.charAt(index) != 'x'){
return x; //...just return what we got.
}
//When we see an 'x' but we aren't at the end...
if(index < str.length() - 1 && str.charAt(index) == 'x'){
x += 'x'; //...append 'x' to the 'x' String.
}
return getX(str, x, index + 1); //recur, moving the index up
}
public String getNonX(String str, String nonX, int index) {
//We are at the end, and if the last char IS NOT an 'x'...
if(index == str.length() - 1 && str.charAt(index) != 'x'){
return (nonX + str.charAt(index)); //...append that char to the 'nonX' String
}
//We are at the end and if the last char IS an 'x'...
else if(index == str.length() - 1 && str.charAt(index) == 'x'){
return nonX; //...just return what we got.
}
//When we see a non 'x' char and we aren't at the end...
if(index < str.length() - 1 && str.charAt(index) != 'x'){
nonX += str.charAt(index); //...append that char to the 'nonX' String
}
return getNonX(str, nonX, index + 1); //recur, move the index up
}
答案 2 :(得分:-1)
您可以尝试此代码...
public String endX(String str) {
if(str.length() <=1) return str;
if(str.charAt(0) == 'x') return endX(str.substring(1)) + 'x';
return str.charAt(0) + endX(str.substring(1));
}