{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "a3020da0581c42f6b867cc64c1144d75",
  "keywords": "数组,二分查找",
  "title": "搜索旋转排序数组",
  "desc": [
    {
      "content": "\n<p>整数数组 <code>nums</code> 按升序排列，数组中的值 <strong>互不相同</strong> 。</p>\n<p>在传递给函数之前，<code>nums</code> 在预先未知的某个下标 <code>k</code>（<code>0 <= k < nums.length</code>）上进行了 <strong>旋转</strong>，使数组变为\n<code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>（下标 <strong>从 0 开始</strong>\n计数）。例如， <code>[0,1,2,4,5,6,7]</code> 在下标 <code>3</code> 处经旋转后可能变为 <code>[4,5,6,7,0,1,2]</code> 。\n</p>\n<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ，如果 <code>nums</code> 中存在这个目标值\n<code>target</code> ，则返回它的下标，否则返回 <code>-1</code> 。\n</p>\n<p> </p>\n<p><strong>示例 1：</strong></p>\n<pre><strong>输入：</strong>nums = [4,5,6,7,0,1,2], target = 0<strong><br />输出：</strong>4</pre>\n<p><strong>示例 2：</strong></p>\n<pre><strong>输入：</strong>nums = [4,5,6,7,0,1,2], target = 3<strong><br />输出：</strong>-1</pre>\n<p><strong>示例 3：</strong></p>\n<pre><strong>输入：</strong>nums = [1], target = 0<strong><br />输出：</strong>-1</pre>\n<p> </p>\n<p><strong>提示：</strong></p>\n<ul>\n<li><code>1 <= nums.length <= 5000</code></li>\n<li><code>-10^4 <= nums[i] <= 10^4</code></li>\n<li><code>nums</code> 中的每个值都 <strong>独一无二</strong></li>\n<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>\n<li><code>-10^4 <= target <= 10^4</code></li>\n</ul>\n<p> </p>\n<p><strong>进阶：</strong>你可以设计一个时间复杂度为 <code>O(log n)</code> 的解决方案吗？</p>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint search(vector<int> &nums, int target)\n\t{\n\t\tint lo = 0;\n\t\tint hi = nums.size() - 1;\n\t\tfor (lo <= hi)\n\t\t{\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tif (nums[mid] == target)\n\t\t\t{\n\t\t\t\treturn mid;\n\t\t\t}\n\t\t\tif (nums[lo] <= nums[mid])\n\t\t\t{\n\t\t\t\tif (nums[lo] <= target && target < nums[mid])\n\t\t\t\t{\n\t\t\t\t\thi = mid - 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlo = mid + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (nums[mid] < target && target <= nums[hi])\n\t\t\t\t{\n\t\t\t\t\tlo = mid + 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\thi = mid - 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-9480f3a882734848aeea80c4b8e0fa7c",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470220"
}