2014-05-18 49 views
1

我工作的一個遞歸問題從codingbat.com其中規定Codingbat.com endX不解決「其他測試」

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

回答

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

PS:這是WAY WAY比它需要的更長。

/* 
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 
}