{"id":5724,"date":"2021-01-04T11:30:44","date_gmt":"2021-01-04T11:30:44","guid":{"rendered":"https:\/\/blog.gwlin.com\/?p=5724"},"modified":"2023-03-26T08:32:55","modified_gmt":"2023-03-26T08:32:55","slug":"[leetcode \u7cfb\u5217] Median of Two Sorted Arrays \u7684PHP\u89e3\u6cd5","status":"publish","type":"post","link":"https:\/\/www.gwlin.com\/blog\/posts\/5724","title":{"rendered":"[leetcode \u7cfb\u5217] Median of Two Sorted Arrays \u7684PHP\u89e3\u6cd5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"php\" class=\"language-php\">class Solution {\n\n    \/**\n     * @param Integer[] $nums1\n     * @param Integer[] $nums2\n     * @return Float\n     *\/\n    function findMedianSortedArrays($nums1, $nums2) {\n        $len1=count($nums1);\n        $len2=count($nums2);\n        $mid=intdiv($len1+$len2,2)+1;\n        $sortedArr=[];\n        while(true){\n            if(current($nums1)===false){\n                $sortedArr=array_merge($sortedArr,array_slice($nums2,key($nums2),$mid-count($sortedArr)));\n                break;\n            }\n            if(current($nums2)===false){\n                $sortedArr=array_merge($sortedArr,array_slice($nums1,key($nums1),$mid-count($sortedArr)));\n                break;\n            }\n\n            if(current($nums1)&gt;=current($nums2)){\n                $sortedArr[]=current($nums2);\n                next($nums2);\n            }else{\n                $sortedArr[]=current($nums1);\n                next($nums1);\n            }\n\n            if(count($sortedArr)&gt;$mid-1){\n                break;\n            }\n        }\n        if(($len1+$len2)%2){\n            return end($sortedArr);\n        }\n        return (end($sortedArr)+prev($sortedArr))\/2;\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Median of Two Sorted Arrays \u7684PHP\u89e3\u6cd5<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179],"tags":[198,186],"class_list":["post-5724","post","type-post","status-publish","format-standard","hentry","category-notes","tag-leetcode","tag-php"],"_links":{"self":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/5724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/comments?post=5724"}],"version-history":[{"count":0,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/5724\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/media?parent=5724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/categories?post=5724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/tags?post=5724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}