I know a simple dichotomy algorithm, this time looking at the solution of the problem, I found a new continent.
It turns out that two points can still play like this.
class Solution {
public int findString(String[] words, String s) {
int left=0;
int right=words.length-1;
int temp=0;
while(left<=right){
while(left<=right&&words[left].equals("")) left++;
while(left<=right&&words[right].equals("")) right--;
//从两头向中间逼近。
int mid=left+(right-left)/2;
temp=mid;//记录mid的值,以方便后面使用
while(words[mid].equals("")&&mid<right) mid=mid+1;
//这里采用向后的遍历方式,找到不为空的字符串
if(words[mid].equals("")){
//到达了右边界,且为空,则直接调整右边界
right=mid-1;
continue;
}
if(words[mid].equals(s)) return mid;
else if(words[mid].compareTo(s)<0) left=mid+1;
else right=mid-1;
}
return -1;
}
}