{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "787af8f4763b45f7a417b4944bb22aec",
  "keywords": "数组,二分查找,分治",
  "title": "寻找两个正序数组的中位数",
  "desc": [
    {
      "content": "\n<p>给定两个大小分别为 <code>m</code> 和 <code>n</code> 的正序（从小到大）数组 <code>nums1</code> 和 <code>nums2</code>。请你找出并返回这两个正序数组的 <strong>中位数</strong> 。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>nums1 = [1,3], nums2 = [2]<strong><br />输出：</strong>2.00000<strong><br />解释：</strong>合并数组 = [1,2,3] ，中位数 2</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>nums1 = [1,2], nums2 = [3,4]<strong><br />输出：</strong>2.50000<strong><br />解释：</strong>合并数组 = [1,2,3,4] ，中位数 (2 + 3) / 2 = 2.5</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>nums1 = [0,0], nums2 = [0,0]<strong><br />输出：</strong>0.00000</pre><p><strong>示例 4：</strong></p><pre><strong>输入：</strong>nums1 = [], nums2 = [1]<strong><br />输出：</strong>1.00000</pre><p><strong>示例 5：</strong></p><pre><strong>输入：</strong>nums1 = [2], nums2 = []<strong><br />输出：</strong>2.00000</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>nums1.length == m</code></li>\t<li><code>nums2.length == n</code></li>\t<li><code>0 <= m <= 1000</code></li>\t<li><code>0 <= n <= 1000</code></li>\t<li><code>1 <= m + n <= 2000</code></li>\t<li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li></ul><p> </p><p><strong>进阶：</strong>你能设计一个时间复杂度为 <code>O(log (m+n))</code> 的算法解决此问题吗？</p>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "class Solution\n{\npublic:\n\tdouble findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)\n\t{\n\t\tint nums1Size = nums1.size();\n\t\tint nums2Size = nums2.size();\n\t\tint na = nums1Size + nums2Size;\n\t\tint *ns = (int *)malloc(4 * na);\n\t\tint i = 0, j = 0, d = 0;\n\t\tint m = na / 2 + 1;\n\t\twhile (d < m)\n\t\t{\n\t\t\tint n;\n\t\t\tif (i < nums1Size && j < nums2Size)\n\t\t\t{\n\t\t\t\tn = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++];\n\t\t\t}\n\t\t\telse if (i < nums1Size)\n\t\t\t{\n\t\t\t\tn = nums1[i++];\n\t\t\t}\n\t\t\telse if (j < nums2Size)\n\t\t\t{\n\t\t\t\tn = nums2[j++];\n\t\t\t}\n\t\t\tns[d++] = n;\n\t\t}\n\t\tif (na % 2)\n\t\t{\n\t\t\treturn ns[d - 1];\n\t\t}\n\t\treturn (ns[d - 1] + ns[d - 2]) / 2.0;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-a84cd69409ca4eb6b4ce004ce6cb68f4",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600469814"
}