我遇到以下問題。遞歸 - 使用一種方法將字符串輸入的每個字符和最後一個字符的每個字符加上一個方法
遞歸方法公共靜態字符串doSomeMagic(「測試」)應該返回:
TTeesstt
TTeess
TTee
TT
我已經實現了這個行爲是這樣的:
public static String rowFunction(String s) {
String toReturn = new String();
if (!s.isEmpty()) {
toReturn = String.valueOf(s.charAt(0));
toReturn += toReturn + rowFunction(s.substring(1));
}
return toReturn;
}
public static String doSomeMagic(String s) {
String toReturn = new String();
if (!s.isEmpty()) {
toReturn = rowFunction(s) + "\n" + doSomeMagic(s.substring(0, s.length() - 1));
}
return toReturn;
}
怎麼能只達到這個一個功能?有任何想法嗎?
@SotiriosDelimanolis我應該如何重命名它?英語不是我的母語,我不知道如何確定問題的標題。任何意見,將不勝感激。 – Endzeit
@SotiriosDelimanolis哦,當然。你是對的。感謝提示。 – Endzeit
另外,您可能在代碼審查或程序員堆棧交換網站上運氣更好。 –