Java implementation LeetCode 81 search rotation sorted array II (b)

81. Search rotation sorted array II

Suppose ascending order according to the array was rotated in a previously unknown point.

(E.g., array [0,0,1,2,2,5,6] may become [2,5,6,0,0,1,2]).

Write a function to determine whether a given target is present in the array. Returns true if there is, otherwise false.

Example 1:

Input: the nums = [2,5,6,0,0,1,2], target = 0
to true: Output
Example 2:

Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Advanced:

This is an extension of the subject search rotation sorted array, this question is repeated nums may contain elements.
This will affect the time complexity of the program do? What will be the impact, and why?

class Solution {
     public boolean search(int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;
        while (low <= high) {
            // 去重
            while (low < high && nums[low] == nums[low + 1]) {
                low++;
            }
            while (low < high && nums[high] == nums[high - 1]) {
                high--;
            }
            // 以下代码与题目33题一致,无修改地方
            int mid = (low + high) / 2;
            if (nums[mid] == target) {
                return true;
            }
            // 把数组大致分为两组,一组为左侧未旋转有序数组,一组为右侧旋转有序数组
            // 如[3 4 5 1 2], [3,4,5]称为左侧,[1,2]称为右侧

            // 0~mid有序,向后规约条件
            // nums[mid] >= nums[0] 表示0~mid有序
            // target > nums[mid] 表示target位于左侧且大于nums[mid],向后规约
            // target < nums[0] 表示target位于右侧,向后规约
            if (nums[mid] >= nums[0] && (target > nums[mid] || target < nums[0])) {
                low = mid + 1;
            } else if (nums[mid] < nums[0] && target > nums[mid] && target < nums[0]) { // 0~mid无序(即包含翻转点),向后规约条件
                // nums[mid] < nums[0] 表示nums[mid]位于右侧
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return false;
    }
}
Released 1197 original articles · won praise 10000 + · views 560 000 +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104349456