{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "4296c87b043a4c909a7910782aa5901c",
  "keywords": "数组,二分查找",
  "title": "搜索旋转排序数组 II",
  "desc": [
    {
      "content": "\n<p>已知存在一个按非降序排列的整数数组 <code>nums</code> ，数组中的值不必互不相同。</p>\n<p>在传递给函数之前，<code>nums</code> 在预先未知的某个下标 <code>k</code>（<code>0 <= k < nums.length</code>）上进行了 <strong>旋转\n</strong>，使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>（下标 <strong>从 0\n开始</strong> 计数）。例如， <code>[0,1,2,4,4,4,5,6,6,7]</code> 在下标 <code>5</code> 处经旋转后可能变为\n<code>[4,5,6,6,7,0,1,2,4,4]</code> 。\n</p>\n<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ，请你编写一个函数来判断给定的目标值是否存在于数组中。如果\n<code>nums</code> 中存在这个目标值 <code>target</code> ，则返回 <code>true</code> ，否则返回 <code>false</code> 。\n</p>\n<p> </p>\n<p><strong>示例 1：</strong></p>\n<pre><strong>输入：</strong>nums = [2,5,6,0,0,1,2], target = 0<strong><br />输出：</strong>true</pre>\n<p><strong>示例 2：</strong></p>\n<pre><strong>输入：</strong>nums = [2,5,6,0,0,1,2], target = 3<strong><br />输出：</strong>false</pre>\n<p> </p>\n<p><strong>提示：</strong></p>\n<ul>\n<li><code>1 <= nums.length <= 5000</code></li>\n<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>\n<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>\n<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>\n</ul>\n<p> </p>\n<p><strong>进阶：</strong></p>\n<ul>\n<li>这是 <a\nhref=\"https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/\">搜索旋转排序数组</a> 的延伸题目，本题中的 <code>nums</code> \n可能包含重复元素。</li>\n<li>这会影响到程序的时间复杂度吗？会有怎样的影响，为什么？</li>\n</ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef search(self, nums, target):\n\t\t\"\"\"\n\t\t:type nums: List[int]\n\t\t:type target: int\n\t\t:rtype: bool\n\t\t\"\"\"\n\t\tdef get(start, end):\n\t\t\tif start > end:\n\t\t\t\treturn False\n\t\t\tmid = (start + end) / 2\n\t\t\tmid = int(mid)\n\t\t\twhile mid < end and nums[mid + 1] == nums[mid]:\n\t\t\t\tmid += 1\n\t\t\twhile start < mid and nums[start + 1] == nums[start]:\n\t\t\t\tstart += 1\n\t\t\tif nums[mid] == target:\n\t\t\t\treturn True\n\t\t\telif mid == end:\n\t\t\t\treturn get(start, mid - 1)\n\t\t\telif start == mid:\n\t\t\t\treturn get(mid + 1, end)\n\t\t\telif nums[mid] >= nums[start]:\n\t\t\t\tif target >= nums[start] and target < nums[mid]:\n\t\t\t\t\treturn get(start, mid - 1)\n\t\t\t\telse:\n\t\t\t\t\treturn get(mid + 1, end)\n\t\t\telif nums[mid] <= nums[end]:\n\t\t\t\tif target > nums[mid] and target <= nums[end]:\n\t\t\t\t\treturn get(mid + 1, end)\n\t\t\t\telse:\n\t\t\t\t\treturn get(start, mid - 1)\n\t\treturn get(0, len(nums) - 1)\n# %%\ns = Solution()\nprint(s.search(nums = [2,5,6,0,0,1,2], target = 0))",
    "language": "python"
  },
  "node_id": "dailycode-a76adb81a0c14ce6ae16b18421469984",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600469924"
}