1.一般解法
int search(int* nums, int numsLen, int target ) {
// write code here
int i = 0;
for(i=0;i<numsLen;i++)
{
if(nums[i]==target)
return i;
}
return -1;
}
2.高效解法
int search(int* nums, int numsLen, int target ) {
// write code here
int left = 0;
int right = numsLen - 1;
int mid = 0;
while(left <=right){
mid = (right - left)/2 + left;
if(nums[mid] == target) return mid;
if(nums[left]<= nums[mid]){//左边是升序的,
if (nums[left] <= target && target <= nums[mid]){
right = mid - 1;
} else{
left = mid +1;
}
}else{
if(nums[mid]<target && target <= nums[right]){
left = mid + 1;
}else{
right = mid - 1;
}
}
}
return -1;
}