继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

leetCode33搜索旋转排序数组

慕尼黑7438881
关注TA
已关注
手记 1
粉丝 1
获赞 1

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;

}

}


打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP