{"id":1316,"date":"2024-11-10T15:16:16","date_gmt":"2024-11-10T07:16:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1316\/"},"modified":"2024-11-10T15:16:16","modified_gmt":"2024-11-10T07:16:16","slug":"median-of-two-sorted-arrays","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1316\/","title":{"rendered":"Median of Two Sorted Arrays"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173036500235792.jpg\" class=\"aligncenter\" title=\"Median of Two Sorted Arrays\u63d2\u56fe\" alt=\"Median of Two Sorted Arrays\u63d2\u56fe\" \/><\/p>\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5927\u5c0f\u5206\u522b\u4e3a m \u548c n \u7684\u6392\u5e8f\u6570\u7ec4 nums1 \u548c nums2\uff0c\u8fd4\u56de\u8fd9\u4e24\u4e2a\u6392\u5e8f\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\u3002<\/p>\n<p>\u6574\u4f53\u8fd0\u884c\u65f6\u95f4\u590d\u6742\u5ea6\u5e94\u4e3a o(log (m+n))<\/p>\n<pre>example 1:\n\ninput: nums1 = [1,3], nums2 = [2]\noutput: 2.00000\nexplanation: merged array = [1,2,3] and median is 2.\nexample 2:\n\ninput: nums1 = [1,2], nums2 = [3,4]\noutput: 2.50000\nexplanation: merged array = [1,2,3,4] and median is (2 + 3) \/ 2 = 2.5.\n\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u9650\u5236\uff1a<\/p>\n<p>nums1.length == m<br \/> nums2.length == n<br \/> 0 0 1 -106 <\/p>\n<pre>var findMedianSortedArrays = function(nums1, nums2) {\n const toltalLength = nums1.length + nums2.length;let x = 0;\nlet y = 0;\nconst mergedArr = []\nfor(let i=0; i&lt; toltalLength;i++){\n\nif(x&gt; nums1.length -1){  nums2.splice(0, y)\n     mergedArr.push(...nums2)\n      break; }\n\n       if(y&gt; nums2.length -1){\n            nums1.splice(0, x)\n            mergedArr.push(...nums1)\n           break;\n       }\n\n       if(nums1[x] &gt; nums2[y]){\n           mergedArr.push(nums2[y]);\n           y++;\n           continue;\n       }else{\n           mergedArr.push(nums1[x]);\n           x++;\n           continue;\n       }\n\n   }\n\n   if(toltalLength % 2 === 0){\n       return (mergedArr[toltalLength\/2] +  mergedArr[toltalLength\/2 -1]) \/2\n   }else{\n       return mergedArr[(toltalLength-1)\/2]\n   }\n\n};\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMedian of Two Sorted Arrays\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e24\u4e2a\u5927\u5c0f\u5206\u522b\u4e3a m \u548c n \u7684\u6392\u5e8f\u6570\u7ec4 nums1 \u548c nums2\uff0c\u8fd4\u56de\u8fd9\u4e24\u4e2a\u6392\u5e8f\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\u3002 \u6574\u4f53\u8fd0\u884c\u65f6\u95f4\u590d\u6742\u5ea6\u5e94\u4e3a o(log (m+n)) example 1: input: nums1 = [1,3], nums2 = [2] output: 2.00000 explanation: merged array = [1,2,3] and median is 2. example 2: input: nums1 = [1,2], nums2 = [3,4] output: 2.50000 explanation: merged array = [1,2,3,4] and median is (2 + 3) \/ 2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1316","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1316"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1316\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}