英文:
My solution throwing exception with corner cases?
问题
给定一个数组,我需要通过递归和二分查找来打印重复给定元素在数组中的第一个索引,但要处理像数组[0]这样的边界情况,它会抛出“ArrayIsOutOfBound”异常。并且即使我需要通过递归和二分查找来查找重复给定元素在数组中的最后一个索引,对于数组长度处的元素也会出现相同的异常。
- 为什么会这样?如何解决这个问题?
要查找最后一个索引-
输入:
int[] array = {5, 10, 10, 10, 10, 20, 20};
int X = 20;
输出应为:
6
我的输出会抛出异常。
我的解决方案是:
public static void main(String[] args) {
int[] arr = {5, 10, 10, 10, 10, 20, 20};
int X = 20;
System.out.println(lastOcc(arr, 0, arr.length - 1, X));
}
public static int lastOcc(int[] arr, int low, int high, int X) {
if (low > high) {
return -1;
}
int mid = low + high / 2;
if (arr[mid] == X) {
if (mid == arr.length - 1 || arr[mid] != arr[mid + 1]) {
return mid;
} else
return lastOcc(arr, mid + 1, high, X);
} else if (arr[mid] > X) {
return lastOcc(arr, low, mid - 1, X);
} else
return lastOcc(arr, mid + 1, high, X);
}
要查找第一个索引-
输入:
int[] arr = {10, 20, 20, 30, 40, 50, 60};
int X = 10;
输出应为:
0
但它会抛出异常。
我的解决方案是:
public static void main(String[] args) {
int[] arr = {10, 20, 20, 30, 40, 50, 60};
int x = 10;
System.out.println(firstOcc(arr, 0, arr.length - 1, x));
}
public static int firstOcc(int[] arr, int low, int high, int x) {
if (low > high) {
return -1;
}
int mid = low + high / 2;
if (arr[mid] == x) {
if (arr[mid - 1] != arr[mid] || mid == 0) {
return mid;
} else
return firstOcc(arr, low, mid - 1, x);
}
if (x > arr[mid]) {
return firstOcc(arr, mid + 1, high, x);
} else
return firstOcc(arr, low, mid - 1, x);
}
英文:
Given an array in which I have to print first index of repeating given element in array through recursion
and binary search but with corner case like for array[0] it is throwing exception ArrayIsOutOfBound. And this is happening in every solution even if I have to find last index of repeating given element in array through recursion and binary search for element at array.length -
- Why? And how to get rid of this?
To find last index-
Input:
int[] array = {5, 10, 10, 10, 10, 20, 20}
int X = 20;
Output should be:
6
My output is throwing exception.
My solution is:
public static void main(String[] args) {
int[] arr = {5, 10, 10, 10, 10, 20, 20};
int X = 20;
System.out.println(lastOcc(arr, 0, arr.length - 1, X));
}
public static int lastOcc(int[] arr, int low, int high, int X) {
if(low > high){
return -1;
}
int mid = low + high / 2;
if(arr[mid] == X) {
if (mid == arr.length - 1 || arr[mid] != arr[mid + 1]) {
return mid;
} else
return lastOcc(arr,mid + 1, high, X);
} else if (arr[mid] > X) {
return lastOcc(arr, low, mid - 1, X);
} else
return lastOcc(arr, mid + 1, high, X);
}
To find first index-
Input:
int[] arr = {10, 20, 20, 30, 40, 50, 60}
int X = 10;
Output should be:
0
But it is throwing exception
My Solution is:
public static void main(String[] args) {
int[] arr = {10, 20, 20, 30, 40, 50, 60};
int x = 10;
System.out.println(firstOcc(arr, 0, arr.length - 1, x));
}
public static int firstOcc(int[] arr, int low, int high, int x){
if(low > high){
return -1;
}
int mid = low + high / 2;
if(arr[mid] == x){
if(arr[mid - 1] != arr[mid] || mid == 0){
return mid;
} else
return firstOcc(arr, low, mid - 1, x);
}
if(x > arr[mid]){
return firstOcc(arr, mid + 1, high, x);
} else
return firstOcc(arr, low, mid - 1, x);
}
答案1
得分: 0
好的,以下是翻译好的内容:
对于此实例,您只需要添加括号
int mid = (low + high) / 2;
更改为
> 在执行加法之前进行除法运算
另外,在 "FirstOccurrence" 中更改
if(arr[mid - 1] != arr[mid] || mid == 0)
为
if(mid == 0 || arr[mid - 1] != arr[mid])
因为这将排除 X=5 的异常情况。
英文:
Well for this instance you only need to add brackets
int mid = (low + high) / 2;
as
> Division is performing prior to addition
And also In "FirstOccurrence" change
if(arr[mid - 1] != arr[mid] || mid == 0) to
if(mid == 0 || arr[mid - 1] != arr[mid] )
as this will omit exception for X=5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论