【发布时间】:2011-05-08 20:26:51
【问题描述】:
这个让我头晕目眩。就在我认为我明白了的时候,我意识到有些不对劲。我必须对这个作业使用递归。有什么提示吗?
/**
* Uses recursion to find index of the shortest string.
* Null strings are treated as infinitely long.
* Implementation notes:
* The base case if lo == hi.
* Use safeStringLength(paths[xxx]) to determine the string length.
* Invoke recursion to test the remaining paths (lo +1)
*/
static int findShortestString(String[] paths, int lo, int hi) {
int min=lo;
if (lo==hi)
return min;
if (safeStringLength(paths[lo]) < safeStringLength(paths[lo+1])){
min=lo;
return Math.min(min, findShortestString(paths, lo+1, hi));
}
else{
min=lo+1;
return Math.min(min, findShortestString(paths, lo+1, hi));
}
}
【问题讨论】:
-
这段代码是你做的吗?或者它是你必须使用的基础?
-
我认为方法需要彻底改变;仅通过
hi和lo连续不给我任何空间在返回的路上给出结果。 -
是的,这就是为什么我说我卡住了..我不知道该怎么办
-
我编辑它有一些进展,但它仍然关闭
-
现在测试我的函数,如果我去掉所有的错别字,它会处理你评论中提到的所有任务。
标签: java arrays algorithm recursion