Langauge: Java
没有语法错误,并且通过了编译器,但是没有通过我的测试。有人能告诉我我哪里做错了吗?
下面是实验文本中关于该方法要做的事情的说明:“计算从索引开始到索引结束的arr子数组中有多少次运行(连续的相等元素块),包括这两个。”
另外:我不允许在这个实验中使用任何循环,但if-else语句是可以的。
参数: arr =数组,start/end = start/end索引
public class Recursion {
int countRuns(int[] arr, int start, int end) {
if (start >= end)
return 0; //checks for null arrays
int counter = 0;
if (start == 0)
counter = 1;
//check the next element for similarity/difference
if (arr[start] != arr[start+1])
counter = 1;
return counter + countRuns(arr, start + 1, end);
}
}https://stackoverflow.com/questions/47664428
复制相似问题