Q4 Median of two sorted arrays
首先奉上題目:
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should beO(log(m+n)) .
Sol 1
看到題目是sorted arrays就覺得應該還挺簡單的,按照樸素思想應該也能解出來,也即將兩個arrays遍歷一半,複雜度應爲
Sol2
根據以往的經驗來說,log(m+n)的複雜度是採用類二分法進行比較。取一組數列的中間項a和另一組數列的中間項b進行比較,若a>b,則取array1的
另外還有一個需要注意的關鍵點是,n+m爲偶數時,中位數爲兩者的均值。