package LeetCode.contact;
class Solution_704 {
public int search(int[] nums, int target) {
int start = 0;
int end = nums.length-1;
for(int i =0; i<(int)(nums.length/2 + 1);i++){
int middle = (int)((end - start +1) /2) + start;
if(nums[middle] == target){
return middle;
}else if(nums[middle] > target){
end = middle-1;
while(end <0 || end >nums.length-1){return -1;}
}else{
start = middle+1;
while(start <0 || start >nums.length-1){return -1;}
}
}
return -1;
}
}
public class leetcode_704 {
public static void main(String[] args){
int[] arr = new int[]{-1,0,3,5,9,12};
Solution_704 sol = new Solution_704();
int a = sol.search(arr, 13);
System.out.println(a);
}
}
|