-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq10p5.java
More file actions
38 lines (37 loc) · 1.17 KB
/
Copy pathq10p5.java
File metadata and controls
38 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
public class q10p5{
public static void main(String[] args){
String[] strs = {"at","","","","ball","","","car","","","dad","",""};
String str = "ball";
System.out.println(search(strs, str, 0, strs.length));
}
public static int search(String[] strs, String str, int start, int end){
if(start > end)
return -1;
int mid = (start + end)/2;
if(strs[mid].isEmpty()){
int left = mid - 1;
int right = mid + 1;
while(true){
if(left<start && right>end){
return -1;
}else if(right <= end && !strs[right].isEmpty()){
mid = right;
break;
}else if(left >= start && !strs[left].isEmpty()){
mid = left;
break;
}
right++;
left--;
}
}
if(str.equals(strs[mid]))
return mid;
else if(strs[mid].compareTo(str) < 0){
return search(strs, str, mid+1, end);
}else{
return search(strs, str, start, mid-1);
}
}
}