{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "5c3fc04cab7e4336b1d589b82d2ddf89",
  "keywords": "数组,二分查找",
  "title": "在排序数组中查找元素的第一个和最后一个位置",
  "desc": [
    {
      "content": "\n<p>给定一个按照升序排列的整数数组 <code>nums</code>，和一个目标值 <code>target</code>。找出给定目标值在数组中的开始位置和结束位置。</p>\n<p>如果数组中不存在目标值 <code>target</code>，返回 <code>[-1, -1]</code>。</p>\n<p><strong>进阶：</strong></p>\n<ul>\n<li>你可以设计并实现时间复杂度为 <code>O(log n)</code> 的算法解决此问题吗？</li>\n</ul>\n<p> </p>\n<p><strong>示例 1：</strong></p>\n<pre><strong>输入：</strong>nums = [5,7,7,8,8,10], target = 8<strong><br />输出：</strong>[3,4]</pre>\n<p><strong>示例 2：</strong></p>\n<pre><strong>输入：</strong>nums = [5,7,7,8,8,10], target = 6<strong><br />输出：</strong>[-1,-1]</pre>\n<p><strong>示例 3：</strong></p>\n<pre><strong>输入：</strong>nums = [], target = 0<strong><br />输出：</strong>[-1,-1]</pre>\n<p> </p>\n<p><strong>提示：</strong></p>\n<ul>\n<li><code>0 <= nums.length <= 10<sup>5</sup></code></li>\n<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>\n<li><code>nums</code> 是一个非递减数组</li>\n<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></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 searchRange(self, nums, target):\n\t\tlength = len(nums)\n\t\tif length == 0:\n\t\t\treturn [-1, -1]\n\t\tmin = 0\n\t\tmax = length - 1\n\t\twhile min <= max:\n\t\t\tpos = (min + max) / 2\n\t\t\tpos = int(pos)\n\t\t\tif nums[pos] > target:\n\t\t\t\tmax = pos - 1\n\t\t\telif nums[pos] < target:\n\t\t\t\tmin = pos + 1\n\t\t\telse:\n\t\t\t\tfor i in range(min, max + 1):\n\t\t\t\t\tif nums[i] == target:\n\t\t\t\t\t\tif min < i and nums[min] != nums[i]:\n\t\t\t\t\t\t\tmin = i\n\t\t\t\t\t\tmax = i\n\t\t\t\treturn [min, max]\n\t\treturn [-1, -1]\n# %%\ns = Solution()\nprint(s.searchRange(nums = [5,7,7,8,8,10], target = 8))",
    "language": "python"
  },
  "node_id": "dailycode-9dc6613c2af04b408404e0c407aaa7c6",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470221"
}