手记

leetCode33搜索旋转排序数组

class Solution {

public int search(int[] nums, int target) {

if(nums.length==0){

                return -1;

            }

            int left=0;

            int right=nums.length-1;

            int mid;

                while(left<right){

                     mid=left+(right-left)/2;

                     if(target==nums[mid]){

                         return mid;

                     }

                    if(nums[left]<=nums[mid]){

                        //前半部分

                        if(target>=nums[left]&&target<nums[mid]){

                            right=mid-1;

                        }else{

                            left=mid+1;

                        }

                    }else{

                        if(target<=nums[right]&&target>nums[mid]){

                            left=mid+1;

                        }else{

                            right=mid-1;

                        }

                    }

                }

                return -1;

}

}


1人推荐
随时随地看视频
慕课网APP