LeetCode之Search in Rotated Sorted Array II

1题目分析

本题是上面的升级版,即搜索含有重复字符的循环数组

2.解题思路

先看version1的题目吧,不含重复字符的循环数组搜索。

思路1.

暴力for循环 AC

思路2.

既然Rotate了,找到突变位置,将所有下标进行平移,再二分查找。

思路3.

其实可以不用平移的,我们看图就明白了(此题升序是隐藏条件,真坑)

就这两种情况。

对着图直接上代码

 int search(vector<int>& nums, int target) {
        int l, r, mid;
        l = 0;
        r = nums.size() - 1;
        while(l <= r){
            mid = (r + l) / 2;
            if(nums[mid] == target){
                return mid;
            }

            if(target > nums[mid]){
                if(nums[r] => target || nums[r] < nums[mid]){
                    l = mid + 1;
                }
                else{
                    r = mid - 1;
                }
            }

            if(target < nums[mid]){
                if(nums[l] <= target || nums[l] > nums[mid]){
                    r = mid - 1;
                }
                else{
                    l = mid + 1;
                }
            }

        }
        return -1;
    }

好,有了version1的思路后,再看看version2。

version2是重复字符的,想象之前的示意图,就是线上多了几个“平台”(平行X轴的线)。而假若平台在中间,不影响判定。假若平台在两边,只需将下标跳过平台,一切就转换为version1了。直接上AC代码。

bool search(vector<int>& nums, int target) {
        int l, r, mid;
        l = 0;
        r = nums.size() - 1;
        while(l <= r){

            mid = (r + l) / 2;
            if(nums[mid] == target){
                return true;
            }

            while(nums[l] == nums[l + 1] && l <= mid){
                l++;
            }
            if(l == mid + 1){
                continue;
            }

            while(nums[r] == nums[r - 1] && r >= mid){
                r--;
            }
            if(r == mid - 1){
                continue;
            }
            mid = (l + r) / 2;

            if(target > nums[mid]){
                if(nums[r] >= target || nums[r] < nums[mid]){
                    l = mid + 1;
                }
                else{
                    r = mid - 1;
                }
            }

            if(target < nums[mid]){
                if(nums[l] <= target || nums[l] > nums[mid]){
                    r = mid - 1;
                }
                else{
                    l = mid + 1;
                }
            }

        }
        return false;
    }
时间: 2024-10-11 23:03:31

LeetCode之Search in Rotated Sorted Array II的相关文章

【leetcode】Search in Rotated Sorted Array II

Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 与I类似,只是在二分搜

【LeetCode】Search in Rotated Sorted Array II (2 solutions)

Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解法一:顺序查找 cl

Java for LeetCode 081 Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 参考Java for LeetCode 033 Search in Rota

LeetCode Solutions : Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Java Solution ( refer to my blog LeetCode So

leetcode No81. Search in Rotated Sorted Array II

Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 在旋转数组中找target,与I不同的是,这次存在重复元素 Algo

[LeetCode][Java] Search in Rotated Sorted Array II

题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题意: 伴随着问题<Search in Rotated Sorted Ar

【leetcode】Search in Rotated Sorted Array II(middle)☆

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 我的思路: 太混乱了 不提了.注意关键区分依据 排好序的一定是从小到大的 看大神的吧: b

leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 思路:此题在解的时候,才发现Search in Rotated Sorted Array

【LeetCode】Search in Rotated Sorted Array II 解题报告

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [解析] 相比Search in Rotated Sorted Array,在

leetcode 【 Search in Rotated Sorted Array II 】python 实现

题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/4254590.html 代码:oj测试通过 Runtime: 73 ms 1 class Solution: 2 # @param A a list of integers 3 # @param target an integer 4 # @return a boolean 5 def search(sel