Leetcode - Search in Rotated Sorted Array II

Leetcode - Search in Rotated Sorted Array II_第1张图片
Paste_Image.png

My code:

public class Solution {
    public boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0)
            return false;
        return search(0, nums.length - 1, nums, target);
    }
    
    private boolean search(int begin, int end, int[] nums, int target) {
        if (begin > end)
            return false;
        else {
            int mid = (begin + end) / 2;
            if (nums[mid] > nums[end]) { // left is sorted
                if (nums[mid] < target) {
                    int i = mid + 1;
                    while (i <= end && nums[i] == nums[i - 1])
                        i++;
                    if (i >= nums.length)
                        return false;
                    else
                        return search(i, end, nums, target);
                }
                else if (nums[mid] > target) {
                    if (nums[begin] > target) { // in the right
                        int i = mid + 1;
                        while (i <= end && nums[i] == nums[i - 1])
                            i++;
                        if (i >= nums.length)
                            return false;
                        else
                            return search(i, end, nums, target);
                    }
                    else if (nums[begin] == target)
                        return true;
                    else { // in the left
                        int i = mid - 1;
                        while (i >= begin && nums[i] == nums[i + 1])
                            i--;
                        if (i < 0)
                            return false;
                        else
                            return search(begin + 1, i, nums, target);
                    }
                }
                else
                    return true;
            }
            else if (nums[mid] < nums[end]) { // right is sorted
                if (nums[mid] > target) {
                    int i = mid - 1;
                    while (i >= begin && nums[i] == nums[i + 1])
                        i--;
                    if (i < 0)
                        return false;
                    else
                        return search(begin, i, nums, target);
                }
                else if (nums[mid] < target) {
                    if (target < nums[end]) {
                        int i = mid + 1;
                        while (i <= end && nums[i] == nums[i - 1])
                            i++;
                        if (i >= nums.length)
                            return false;
                        else
                            return search(i, end, nums, target);
                    }
                    else if (target > nums[end]) {
                        int i = mid - 1;
                        while (i >= begin && nums[i] == nums[i + 1])
                            i--;
                        if (i < 0)
                            return false;
                        else
                            return search(begin, i, nums, target);
                    }
                    else
                        return true;    
                }   
                else
                    return true;
            }
            else
                if (nums[mid] == target)
                    return true;
                else
                    return search(begin, end - 1, nums, target);
        }
    }
}

My test result:

Leetcode - Search in Rotated Sorted Array II_第2张图片

这道题目还是和前面的差不多。然后就是要用while循环把重复的数字跳到。
然后就是同样的注意点,当nums[mid] = nums[end] 时,可能出现两种情况,右侧已经排序好或者左侧已经排序好。所以不能确定。
于是,就直接将end - 1,重新进行搜索。
所以,如果数组数字都是一样的,需要进行 O(n)复杂度,而不再是之前的binary search 的 O(log n)

**
总结: Array, binary search
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0)
            return false;
        int begin = 0;
        int end = nums.length - 1;
        while (begin <= end) {
            int middle = (begin + end) / 2;
            if (nums[middle] < nums[end]) { // right part is sorted
                if (target < nums[middle]) {
                    end = middle - 1;
                }
                else if (target == nums[middle]) {
                    return true;
                }
                else if (target <= nums[end]) {
                    begin = middle + 1;
                }
                else {
                    end = middle - 1;
                }
            }
            else if (nums[middle] > nums[end]) { // left part is sorted
                if (target > nums[middle]) {
                    begin = middle + 1;
                }
                else if (target == nums[middle]) {
                    return true;
                }
                else if (target >= nums[0]) {
                    end = middle - 1;
                }
                else {
                    begin = middle + 1;
                }
            }
            else {
                if (target == nums[middle])
                    return true;
                else
                    end = end - 1;
            }
        }
        return false;
    }
}

差不多的题目。就把之前的情况细分。
nums[middle] < nums[end]
nums[middle] > nums[end]
nums[middle] == nums[end] -> end - 1
差不多。然后因为出现了重复元素所以可以滑动,用while循环加速。
但是我懒得写了。第一次的版本是写了的。

Anyway, Good luck, Richardo!

提供一种新的思路就是直接traverse, 复杂度是 O(n)
上面的做法, time complexity: best: O(log n), worst: O(n)

Anyway, Good luck, Richardo! -- 08/12/2016

My code:

public class Solution {
    public boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return false;
        }
        
        int begin = 0;
        int end = nums.length - 1;
        while (begin <= end) {
            int mid = begin + (end - begin) / 2;
            if (target == nums[mid]) {
                return true;
            }
            else if (nums[begin] < nums[mid]) { // left is sorted
                if (nums[begin] <= target && target < nums[mid]) {
                    end = mid - 1;
                }
                else {
                    begin = mid + 1;
                }
            }
            else if (nums[begin] > nums[mid]) { // right is sorted
                if (nums[mid] < target && target <= nums[end]) {
                    begin = mid + 1;
                }
                else {
                    end = mid - 1;
                }
            }
            else {
                begin++;
            }
        }
        return false;
    }
}

reference:
https://discuss.leetcode.com/topic/310/when-there-are-duplicates-the-worst-case-is-o-n-could-we-do-better/3

much simpler code

首先判断,左右那个部分是 sorted ,然后再进行下一步。

Anyway, Good luck, Richardo! -- 09/21/2016

你可能感兴趣的:(Leetcode - Search in Rotated Sorted Array II)