public int findString(String[] str, int n, String x) {
int left = 0,right=n;
while (true) {
if(right - left <= 0){
break;
}
int mid = (left + right) / 2;
if (str[mid].equals(x)) {
return mid;
} else {
if (str[mid] == "") {
for(int i=mid-1;i>=0;i--){
if(str[i].equals("")){
continue;
} else {
if(str[i].compareTo(x) > 0){
right = i;
continue;
} else {
if(str[i].compareTo(x)<0){
left = mid+1;
continue;} else {
return i;
}
}
}
}
} else {
if (str[mid].compareTo(x) > 0) {
right = mid;
continue;
} else {
left = mid + 1;
continue;
}
}
}
}
return -1;
}
在有序但含有空的数组中查找字符串
最新推荐文章于 2020-01-11 18:38:41 发布