查找出现最小值的数组索引
这让我头晕目眩。正当我以为我明白了的时候,我意识到有些不对劲。我必须使用递归来完成这项作业。有什么提示吗?
/**
* 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));
}
}
This one is making my head spin. Just when I think I got it, I realize something's not right. I have to use recursion for this assignment. Any hints?
/**
* 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));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为这里有一些东西:
解释为什么它有效:
这是一个示例:
显然,索引 2 是最短的。
自下而上思考。从下往上阅读以下内容。
我让它听起来像是每个函数都在与链中位于其上方的函数进行对话。
每行由传递的函数参数引导。
所以在代码中,您会看到确实发生了这种情况。
当我们定义
ShortestIndexSoFar
时,每个函数都会知道它之外的所有路径中最短的一条。就在它之后,函数本身检查其索引是否具有比下面所有路径中最短的路径更短的路径。
继续将最短的索引向上滴,最后一个人将返回最短的索引。
这有道理吗?
I think got something here:
Explaining why this works:
Here's a sample:
Obviously, index 2 is the shortest one.
Think bottoms up. Read the following starting from the bottom, upwards.
I make it sound like each function is talking to the function above it in the chain.
And each line is lead by the function parameters that were passed.
So in the code, you see that exactly happening.
When we define
ShortestIndexSoFar
, this is where each function will know the shortest of all the paths beyond it.And right after it is where the function itself checks if its index has a shorter path than the shortest of all the ones below.
Keep trickling the shortest one upward, and the final guy will return the shortest index.
That makes sense?
由于这是家庭作业,这里有一个帮助您学习的提示:
对于OP以外的人...请不要给出答案...例如通过更正您的答案!
Since this is homework, here's a hint to help you learn:
To people other than the OP ... PLEASE DON'T GIVE THE ANSWERS AWAY ... e.g. by correcting your answers!
为什么不直接获取每个元素的长度并对返回的长度进行排序以获得排序?像这样。
Why not just get the length of each element and sort the returned length to get the ordering? Like this.
一种解决方案是这样的
one solution will be like this
一个简单的解决方案:
A simple solution:
根据运行
findShortestString
的结果计算min
没有意义。开始此类问题的最佳方法是仅考虑单个递归步骤,您可以通过考虑仅包含两个要比较的字符串的数组会发生什么来做到这一点。您要做的就是检查第一个字符串的长度与第二个字符串的长度。但真正的技巧是,您想通过递归调用该函数来测试第二个的长度。这很简单,但需要确定递归的最终情况。当
lo == hi
时,您已成功完成此操作。也就是说,当lo == hi
时,最短的已知字符串是lo
(它是唯一已知的字符串!)。好的,回到比较两个字符串。鉴于您知道要比较路径中存储的两个字符串的长度,您可能会执行以下操作(不使用递归):
但是您想要递归 - 并且在递归步骤中您需要以某种方式生成
paths[1]
的1
。我们已经弄清楚了如何做到这一点,当lo == hi
时,我们返回lo
。因此,递归步骤是“将当前已知的最低字符串长度与已知最佳索引的字符串长度进行比较”——等等,我们有一个函数可以实现这一点!它是findShortestString
。因此我们可以将上面的内容修改得更加简洁,并在基本情况中添加得到:Calculating
min
on the result of runningfindShortestString
isn't meaningful. The best way to start this kind of problem is to consider just a single recursive step, you can do this by considering what happens with an array of only two strings to compare.What you want to do is check the length of the first string against the length of the second. The real trick, though, is that you want to test the length of the second by calling the function recursively. This is straight forward enough, but requires determining the end-case of your recursion. You did this successfully, it's when
lo == hi
. That is, whenlo == hi
the shortest known string islo
(it's the only known string!).Ok, so back to comparing just two strings. Given that you know that you want to compare the length of two strings stored in
paths
, you might do something like this (without recursion):But you want to recurse -- and in the recurse step you need to somehow generate that
1
ofpaths[1]
. We already figured out how to do that, whenlo == hi
, we returnlo
. Thus the recursion step is "compare the current lowest known string length to the string length of the best known index" -- wait, we have a function for that! it'sfindShortestString
. Thus we can modify what's written above to be slightly more concise, and add in the base case to get: