LeetCode 81. 搜索旋转排序数组 II

题目连接

81. 搜索旋转排序数组 II

题目思路

参考LeetCode 33的思想

代码实现

class Solution {
    public boolean search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while(left <= right){
            int mid = left + ((right - left) >> 1);
            if(nums[mid] == target){
                return true;
            }
            if(nums[mid] < nums[right]){
                if(nums[mid] < target){
                    if(target <= nums[right]){
                        left = mid + 1;
                    }else{
                        right = mid - 1;
                    }
                }else{
                    right = mid - 1;
                }
            }else if(nums[mid] > nums[right]){
                if(nums[mid] < target){
                    left = mid + 1;
                }else{
                    if(nums[right] < target){
                        right = mid - 1;
                    }else{
                        left = mid + 1;
                    }
                }
            }else{
                right--;
            }
        }
        return false;
    }
}
posted @ 2020-10-05 17:14  ZJPang  阅读(82)  评论(0编辑  收藏  举报