leetCode33搜尋旋轉排序陣列

lee_lgw發表於2021-09-09

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;

}

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1020/viewspace-2824691/,如需轉載,請註明出處,否則將追究法律責任。

相關文章