This question already has an answer here:
What does “possible lossy conversion” mean and how do I fix it?
(1个答案)
12个月前关闭。
(1个答案)
12个月前关闭。
/**
*
* @param b is an array
* @param x is the element for which I want to search the lower bound
* @param n is the length of the array
* @return high if element not found or mid - 1 if found
*/
static long searchMe(long b[], long x, long n){
long low = 0, high = n, mid=0;
while(low<=high){
mid = (low+high)/2;
if(b[mid] == x){
if(mid > 0 && b[mid-1] == x) high = mid-1;
else return mid-1;
}
else if(b[mid] < x) low = mid + 1;
else high = mid - 1;
}
// System.out.println(low + " == " + high);
return high;
}
最佳答案
您正在使用mid作为数组的索引,但是mid很长。数组的索引始终是一个整数。你可以试试看
static long searchMe(long b[], long x, long n) {
long low = 0, high = n;
int mid = 0; // CHANGED FROM long TO int
while (low <= high) {
mid = (int) ((low + high) / 2); // CAST to int
if (b[mid] == x) {
if (mid > 0 && b[mid - 1] == x)
high = mid - 1;
else
return mid - 1;
} else if (b[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
// System.out.println(low + " == " + high);
return high;
}
07-24 09:15