-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ex29.java 压根就不需要跑就知道有问题,问题都没搞清楚 #14
Comments
是 1.1.29 吗?请问具体问题是什么呢?@RealZark |
1.1.29 rank是有问题的,比如你测试key为14,你返回的pos是-1,其实应该是 hi + 1; |
这个应该是一个正确的解,但是不是最优解 public static int rank(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > key) {
hi = mid - 1;
} else if (a[mid] < key) {
lo = mid + 1;
} else {
// find, look backward
--mid;
while (mid >= lo && a[mid] == key) {
--mid;
}
return mid + 1;
}
}
// not found
return hi + 1;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: