線形検索関数を変更して、ベクトル内のターゲットの最後の出現を見つける関数を作成しようとしています。
private int linearSearchRecursive(int[] input, int key,int index) {
if (index == 0) {
return -1;
}
if (input[index] == key) {
return index;
}
else
return linearSearchRecursive(input,key,--index);
}
ヘルパー関数を使用して機能させる方法を考えました...
public static int findLastOccurance(int[] items, int key){
return linearSearchRecursive(items, key, items.length - 1);
}
または、そのような性質のものですが、再帰性を維持しながら 1 つの関数のみを使用できる簡単な方法があるかどうか疑問に思っていましたか?