Skip to content
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

关于数据结构-二叉树-二分查找代码的问题 #44

Open
bnjsj opened this issue Sep 6, 2019 · 0 comments
Open

关于数据结构-二叉树-二分查找代码的问题 #44

bnjsj opened this issue Sep 6, 2019 · 0 comments

Comments

@bnjsj
Copy link

bnjsj commented Sep 6, 2019

针对文中代码修改如下,见注释处
private static int search(int[] data,int l,int r,int target){
int mid;
//注意:此处循环条件为<=,若无=则无法查找数组起始处数据
while(l<=r){
mid=(l+r)/2;
if(data[mid]==target){
return mid;
}else if(data[mid]<target){
l=mid+1;
}else{
r=mid;
}
}
return -1;
}
private static int searchDfs(int[] data,int l,int r,int target){
//注意:此处循环条件为>,若有=则无法查找数组起始处数据
if(l>r){
return -1;
}
int mid=(l+r)/2;
if(target==data[mid]){
return mid;
}else if(target>data[mid]){
return searchDfs(data,mid+1,r,target);
}else{
return searchDfs(data,l,mid,target);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant