solution.json 3.8 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": "1472af7b2fbd488290d393506127b757",
  "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": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\nstatic bool search(int *nums, int numsSize, int target)\n{\n\tint lo = 0;\n\tint hi = numsSize - 1;\n\twhile (lo <= hi)\n\t{\n\t\tint mid = lo + (hi - lo) / 2;\n\t\tif (nums[mid] == target)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\tif (nums[lo] == nums[mid] && nums[mid] == nums[hi])\n\t\t{\n\t\t\tlo++;\n\t\t\thi--;\n\t\t}\n\t\telse if (nums[lo] <= nums[mid])\n\t\t{\n\t\t\tif (nums[lo] <= target && target < nums[mid])\n\t\t\t{\n\t\t\t\thi = mid - 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tlo = mid + 1;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (nums[mid] < target && target <= nums[hi])\n\t\t\t{\n\t\t\t\tlo = mid + 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\thi = mid - 1;\n\t\t\t}\n\t\t}\n\t}\n\treturn false;\n}\nint main(int argc, char **argv)\n{\n\tint i;\n\tint target = atoi(argv[1]);\n\tint size = argc - 2;\n\tint *nums = malloc(size * sizeof(int));\n\tfor (i = 0; i < argc - 2; i++)\n\t{\n\t\tnums[i] = atoi(argv[i + 2]);\n\t}\n\tprintf(\"%d\\n\", search(nums, size, target));\n\treturn 0;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-248163efec6747ce852e85ea2dbe3c15",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600469924"
}