【发布时间】:2019-07-06 17:48:19
【问题描述】:
我想知道 find 和 replaceAll 方法的最佳、最差和平均情况以及增长函数,它基本上是在以下代码中数组大小大于零的每种情况下执行的语句数
/**
* Return index where value is found in array or -1 if not found.
* @param array ints where value may be found
* @param value int that may be in array
* @return index where value is found or -1 if not found
*/
public static int find(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
return i;
}
}
return -1;
}
/**
* Replace all occurrences of oldValue with newValue in array.
* @param array ints where oldValue may be found
* @param oldValue value to replace
* @param newValue new value
*/
public static void replaceAll(int[] array, int oldValue, int newValue) {
int index = find(array, oldValue);
while (index > -1) {
array[index] = newValue;
index = find(array, oldValue);
}
}
【问题讨论】:
-
您认为两种方法中最好的最坏情况和平均情况是什么?您为什么这么认为?
-
对于 find():1- 如果元素在数组的第一个索引中,则为最佳情况 2- 最坏情况:如果该元素在数组中不存在。 3-平均:如果元素在数组的中间。这就是我的想法。我也想找到增长函数
-
编辑您的问题以包含该问题以及您要查找的“增长函数”是什么?
标签: java algorithm replace find