import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @param target int整型
* @return int整型
*/
public int search (int[] nums, int target) {
// write code here
int position=search_target(nums,0,nums.length-1,target);
return position;
}
public int search_target(int[] array,int left,int right,int target){
if(array.length<1){return -1;}
int position=-1;
if((right==left+1)&&(array[left]!=target)&&(array[right]!=target)){return -1;}
if(array[left]==target){return left;}
if(array[right]==target){return right;}//两种直接输出的case
if(array[left]<target&&array[right]>target){//target位于之间
int mid=(left+right)/2;
if(array[mid]==target){return mid;}
else if(array[mid]>target){//target在左侧
right=mid;
position=search_target(array,left,right,target);
}
else if(array[mid]<target){//target在右侧
left=mid;
position=search_target(array,left,right,target);
}
}
if(array[left]>target||array[right]<target){return -1;}
return position;
}
}
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @param target int整型
* @return int整型
*/
public int search (int[] nums, int target) {
// write code here
int position=search_target(nums,0,nums.length-1,target);
return position;
}
public int search_target(int[] array,int left,int right,int target){
if(array.length<1){return -1;}
int position=-1;
if((right==left+1)&&(array[left]!=target)&&(array[right]!=target)){return -1;}
if(array[left]==target){return left;}
if(array[right]==target){return right;}//两种直接输出的case
if(array[left]<target&&array[right]>target){//target位于之间
int mid=(left+right)/2;
if(array[mid]==target){return mid;}
else if(array[mid]>target){//target在左侧
right=mid;
position=search_target(array,left,right,target);
}
else if(array[mid]<target){//target在右侧
left=mid;
position=search_target(array,left,right,target);
}
}
if(array[left]>target||array[right]<target){return -1;}
return position;
}
}