solution.json 3.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "b79586225ee645c59544bd27132cb5dc",
  "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": "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\tvector<int> searchRange(vector<int> &nums, int target)\n\t{\n\t\tvector<int> res;\n\t\tres.push_back(binary_search_begin(nums, target));\n\t\tres.push_back(binary_search_end(nums, target));\n\t\treturn res;\n\t}\nprivate:\n\tint binary_search_begin(vector<int> nums, int target)\n\t{\n\t\tint lo = -1;\n\t\tint hi = nums.size();\n\t\twhile (lo + 1 < hi)\n\t\t{\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tif (target > nums[mid])\n\t\t\t{\n\t\t\t\tlo = mid;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\thi = mid;\n\t\t\t}\n\t\t}\n\t\tif (hi == nums.size() || nums[hi] != target)\n\t\t{\n\t\t\treturn -1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn hi;\n\t\t}\n\t}\n\tint binary_search_end(vector<int> nums, int target)\n\t{\n\t\tint lo = -1;\n\t\tint hi = nums.size();\n\t\twhile (lo + 1 < hi)\n\t\t{\n\t\t\tint mid = lo + (hi - lo) / 2;\n\t\t\tif (target < nums[mid])\n\t\t\t{\n\t\t\t\thi = mid;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tlo = mid;\n\t\t\t}\n\t\t}\n\t\tif (lo == -1 || nums[lo] != target)\n\t\t{\n\t\t\treturn -1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn lo;\n\t\t}\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-09529ae417314d8cb84c07be9aec2849",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470221"
}