solution.json 2.0 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": "csdn.net",
  "source": "solution.md",
  "exercise_id": "383deea124124fc3af6f677fa1cf9305",
  "keywords": "数组,二分查找",
  "title": "搜索插入位置",
  "desc": [
    {
      "content": "\n<p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p><p>你可以假设数组中无重复元素。</p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 5<strong><br />输出:</strong> 2</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 2<strong><br />输出:</strong> 1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 7<strong><br />输出:</strong> 4</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong> [1,3,5,6], 0<strong><br />输出:</strong> 0</pre>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic int searchInsert(int[] nums, int target) {\n\t\tint left = 0, right = nums.length - 1;\n\t\tif (target < nums[left])\n\t\t\treturn 0;\n\t\tif (target > nums[right])\n\t\t\treturn nums.length;\n\t\twhile (left <= right) {\n\t\t\tint mid = (right - left) / 2 + left;\n\t\t\tif (target < nums[mid]) {\n\t\t\t\tright = mid - 1;\n\t\t\t} else if (target > nums[mid]) {\n\t\t\t\tleft = mid + 1;\n\t\t\t} else {\n\t\t\t\treturn mid;\n\t\t\t}\n\t\t}\n\t\treturn left;\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-00a267eca7a84efdac846774beeb5766",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600470222"
}