Java 作业帮助 - 数组递归
我有一组需要解决的递归问题。我已经完成了给我们的 4 个任务中的 3 个,但我很难理解最后一个任务。 我不一定想要实际的答案,但也许只是为我指明正确的方向,因为我什至不知道我的停止条件应该是什么。请注意,它必须是递归的,没有循环等。
提前感谢您提供的任何帮助!
编写递归方法 arrayRange,返回填充的整数数组中的最大整数减去最小整数。使用递归;不要使用循环。必须通过以下断言(请注意传递对新数组的引用的快捷方式 - 它可以节省您编写一些代码(这传递作为参数构建的数组):
assertEquals(2, rf.arrayRange(new int[] { 1, 2, 3 } ));
assertEquals(2, rf.arrayRange(new int[] { 3, 2, 1 } ));
assertEquals(0, rf.arrayRange(new int[] { 3 } ));
assertEquals(3, rf.arrayRange(new int[] { -3, -2, -5, -4 } ));
// 前提条件:a.length > 0 公共 int arrayRange(int[] a)
I've got a set of recursion problems that I need to do. I've completed 3 out of the 4 of them we were given, but I'm having a hard time wrapping my head around this last one. I don't necessarily want the actual answer, but maybe just point me in the right direction, because I'm not even seeing what my stop condition should be on this one. And note, it has to be recursive, no loops, etc.
Thanks in advance for any help provided!
Write recursive method arrayRange that returns the maximum integer minus the minimum integer in the filled array of ints. Use recursion; do not use a loop. The following assertions must pass (note the shortcut way to pass a reference to a new array--it saves your writing a bit of code (this passes an array built as a parameter):
assertEquals(2, rf.arrayRange(new int[] { 1, 2, 3 } ));
assertEquals(2, rf.arrayRange(new int[] { 3, 2, 1 } ));
assertEquals(0, rf.arrayRange(new int[] { 3 } ));
assertEquals(3, rf.arrayRange(new int[] { -3, -2, -5, -4 } ));
// Precondition: a.length > 0
public int arrayRange(int[] a)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
停止条件是只剩下两项:最大值和最小值。然后只需返回差额即可。 (还处理 1 或 0 项的情况,考虑测试用例中的输入。)
现在..如何减少每次传递的列表? :) 我会考虑一次检查前三个值(在这三个值中,只有两个应该保留在递归步骤中)。
快乐的家庭作业。
The stop condition is when there are only two items left: the maximum and minimum. Then just return the difference. (Also handle the case of 1 or 0 items, consider input such as in the test cases.)
Now .. how to reduce the list each pass? :) I would consider inspecting the first three values at a time (of the three, only two should remain in the recursive step).
Happy homeworking.