Search in Rotated Sorted Array II

原创 2016年08月30日 15:15:04

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.

允许重复元素,则上一题中的条件A[mid] >= A[first]将不能确定[first, mid]为递增序列,如[3,5,3,3,3,3,3]。
所以判断:

  • A[mid]>A[first],则区间一定递增。
  • A[mid] == A[first],什么都确定不了,所以first指针前移一位,越过重复元素。

C++

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        int first = 0;
        int last = nums.size();
        while(first != last)
        {
            const int mid = first + (last - first) / 2;
            if(nums[mid] == target)
                return true;
            if(nums[first] < nums[mid])
            {
                if(nums[first] <= target && target < nums[mid])
                    last = mid;
                else
                    first = mid + 1;
            }
            else if(nums[first] > nums[mid])
            {
                if(nums[mid] < target && target <= nums[last-1])
                    first = mid + 1;
                else
                    last = mid;
            }
            else
                first++;
        }
        return false;
    }
};

Java

public class Solution {
    public boolean search(int[] nums, int target) {
        int first = 0;
        int last = nums.length;
        while(first != last)
        {
            int mid = first + (last - first) / 2;
            if(nums[mid] == target)
                return true;
            if(nums[first] < nums[mid])
            {
                if(nums[first] <= target && target < nums[mid])
                    last = mid;
                else
                    first = mid + 1;
            }
            else if(nums[first] > nums[mid])
            {
                if(nums[mid] < target && target <= nums[last-1])
                    first = mid + 1;
                else
                    last = mid;
            }
            else
                first++;
        }
        return false;
    }
}

Python

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: bool
        """
        first = 0
        last = len(nums)
        while(first != last):
            mid = first + (last - first) / 2;
            if(nums[mid] == target):
                return True
            if(nums[mid] > nums[first]):
                if(nums[first] <= target and target < nums[mid]):
                    last = mid
                else:
                    first = mid + 1
            elif(nums[mid] < nums[first]):
                if(nums[mid] < target and target <= nums[last-1]):
                    first = mid + 1
                else:
                    last = mid
            else:
                first += 1
        return False
版权声明:本文为博主原创文章,未经博主允许不得转载。

【LeetCode-面试算法经典-Java实现】【153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)】

【153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Suppose...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月21日 06:24
  • 1830

循环有序数组中的二分查找 Search in a rotated sorted array

二分查找是必须要掌握的技能。二分查找适用于有序的、顺序的存储结构。 1、可以用它来查找某一个数 2、可以用于查找某一个范围。如《二分查找有序数组中某个数的所在范围 Search for a Rang...
  • luckyjoy521
  • luckyjoy521
  • 2013年12月22日 19:16
  • 2101

【LeetCode-面试算法经典-Java实现】【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】

【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Follow up fo...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月04日 06:47
  • 1275

[leetcode]81. Search in Rotated Sorted Array II(Java)

https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description Follow up f...
  • zjkC050818
  • zjkC050818
  • 2017年07月02日 10:41
  • 128

81. Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affec...
  • x_shuck
  • x_shuck
  • 2016年02月19日 20:48
  • 213

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

题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t...
  • Evan123mg
  • Evan123mg
  • 2015年07月18日 14:59
  • 464

LeetCode81——Search in Rotated Sorted Array II

LeetCode81——Search in Rotated Sorted Array II
  • zhangxiao93
  • zhangxiao93
  • 2016年01月26日 23:06
  • 779

[leetcode] 81.Search in Rotated Sorted Array II

题目: Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affe...
  • u014673347
  • u014673347
  • 2015年06月11日 16:32
  • 209

LeetCode 之 Search in Rotated Sorted Array I II — C/C++ 实现

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you befo...
  • abc123man
  • abc123man
  • 2015年06月11日 14:09
  • 225

【LeetCode从零单刷】Search in Rotated Sorted Array I & II

I 题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 ...
  • yOung_One
  • yOung_One
  • 2015年11月02日 23:18
  • 1175
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Search in Rotated Sorted Array II
举报原因:
原因补充:

(最多只允许输入30个字)