- 做者:陳大魚頭
- 項目地址:ying-leetcode
- 碎碎念:Mmmmm,不按期刷leetcode,會以JS TS PY的形式輸出出來
給定兩個大小爲 m 和 n 的有序數組 nums1 和 nums2。javascript
請你找出這兩個有序數組的中位數,而且要求算法的時間複雜度爲 O(log(m + n))。java
你能夠假設 nums1 和 nums2 不會同時爲空。git
示例 1:github
nums1 = [1, 3]
nums2 = [2]
// 則中位數是 2.0
複製代碼
示例 2:算法
nums1 = [1, 2]
nums2 = [3, 4]
// 則中位數是 (2 + 3)/2 = 2.5
複製代碼
這條題目自己其實不難,主要麻煩的地方是限制了時間複雜度爲 O(log(m + n)),因此咱們能夠參考歸併排序的思路去實現,那麼既然是歸併排序,就有兩種方案,一種是自上而下的遞歸,另外一種是 自下而上的迭代。typescript
其實就是對排好序的兩個數組的掃描,判斷,一直往右移,每移一次判斷一次當前位置,若是兩個數組總長度是奇數,事實上這個中位數就是在兩個數組合並以後的最中間的值,不然則是中間位置先後的平均值,由於在這裏咱們不能合併數組,因此不能判斷出結果。可是咱們能夠記錄每次遍歷兩個數組的位置,從而不斷縮小這個中位數的區間。數組
那麼關鍵步驟就是:微信
Max(left1, left2)
(Max(left1, left2) + Max(right1, right2)) / 2
/** * @func * @name findMedian * @desc 使用二分法來獲取中位數 * @param {number} start1 第一個數組須要查找的起始位置 * @param {number[]} nums1 傳入的第一個數組 * @param {number} start2 第二個數組須要查找的起始位置 * @param {number[]} nums2 傳入的第二個數組 * @param {number} half nums1與nums2的長度中間值 * @return {number} nums1與nums2的中位數 */
const findMedian = (start1, nums1, start2, nums2, half) => {
const [{ length: len1 }, { length: len2 }] = [nums1, nums2]
// 下面兩個if主要是對空數組的判斷,若是是有一個是空數組,則輸出另外一個數組的中位數
if (start1 >= len1) {
return nums2[start2 + half - 1]
}
if (start2 >= len2) {
return nums1[start1 + half - 1]
}
if (half === 1) { // 此時兩個數組總長度的一半就是等於1,說明兩個數組就只有一個值,那麼就哪一個小輸出哪一個
return Math.min(nums1[start1], nums2[start2])
}
// 下面的不變量定義主要是爲了查找是否有中間值,若是沒有就賦值爲Infinity,不然則賦值爲這個中間值
const halfVal1 = (start1 + parseInt(half / 2) - 1 < len1
? nums1[start1 + parseInt(half / 2) - 1]
: Infinity)
const halfVal2 = (start2 + parseInt(half / 2) - 1 < len2
? nums2[start2 + parseInt(half / 2) - 1]
: Infinity)
// 這裏是二分法的核心,判斷兩個數組中間值的大小,若是nums1的(half / 2)比較小的話,就說明咱們找的數字不在nums1的前(half / 2)數字之中,因此咱們nums1就日後移動(half / 2)。反之就是nums2日後移動(half / 2)。一直用二分法遞歸對比沒有判斷過的數,從而獲得結果值。
return (halfVal1 < halfVal2
? findMedian(start1 + parseInt(half / 2), nums1, start2, nums2, half - parseInt(half / 2))
: findMedian(start1, nums1, start2 + parseInt(half / 2), nums2, half - parseInt(half / 2)));
}
/** * @func * @name findMedianSortedArrays * @desc 尋找兩個有序數組的中位數 * @param {number[]} nums1 傳入的第一個數組 * @param {number[]} nums2 傳入的第二個數組 * @return {number} 中位數 */
const findMedianSortedArrays = (nums1, nums2) => {
const [{ length: len1 }, { length: len2 }] = [nums1, nums2]
const totalLen = len1 + len2 // 兩個數組的總長度
return (totalLen % 2 === 1
? findMedian(0, nums1, 0, nums2, (totalLen + 1) / 2) // 總長度爲奇數時
: (findMedian(0, nums1, 0, nums2, totalLen / 2) + findMedian(0, nums1, 0, nums2, totalLen / 2 + 1)) / 2) // 總長度爲偶數時
}
/** * @func * @name findMedianSortedArrays2 * @desc 尋找兩個有序數組的中位數 * @param {number[]} nums1 傳入的第一個數組 * @param {number[]} nums2 傳入的第二個數組 * @return {number} 中位數 */
const findMedianSortedArrays2 = (nums1, nums2) => {
let len1 = nums1.length
let len2 = nums2.length
// 下面交換法主要是爲了統一一個方向,方便迭代
if (len1 > len2) {
let lenTemp = len1
let numsTemp = nums1
len1 = len2
len2 = lenTemp
nums1 = nums2
nums2 = numsTemp
}
let left = 0 // 左邊開始的位置
let right = len1 // 右邊開始的位置
let mid = Math.floor((len1 + len2 + 1) / 2) // 中間位置
// 二分法 迭代模式
while (left <= right) {
let i = Math.floor((left + right) / 2) // 左邊遍歷到的位置
let j = Math.floor(mid - i) // 右邊遍歷到的位置
if (i < len1 && nums2[j - 1] > nums1[i]) { // 若是當前左邊遍歷的位置還沒到數組1的長度,而且數組二遍歷到的元素是比數組已的元素大的,那麼這該時候就說明i還過小,須要+1
left = i + 1
} else if (i > 0 && nums1[i - 1] > nums2[j]) { // 如上,相反的邏輯 - 1
right = i - 1
} else {
let maxLeft
let maxRight
// 下面一層二層的判斷是爲了處理當有一個數組長度爲0時的狀況
if (i === 0) {
maxLeft = nums2[j - 1]
} else if (j === 0) {
maxLeft = nums1[i - 1]
} else { // 此時則是獲取當前遍歷到的兩個數組對比的最大值
maxLeft = Math.max(nums1[i - 1], nums2[j - 1])
}
if ((len1 + len2) % 2 === 1) { // 若是兩個數組的長度爲奇數,能夠直接輸出最大值
return maxLeft
}
// 下面則是判斷數組長度加起來爲偶數時的狀況
if (i === len1) { // 當i已經遍歷完時,則右邊中間值爲第二個數組當前遍歷的值
maxRight = nums2[j]
} else if (j === len2) { // 與上相反
maxRight = nums1[i]
} else { // 獲取當前遍歷到的值中的最大值
maxRight = Math.min(nums1[i], nums2[j])
}
// 最終的值爲left right兩邊中間值的一半
return (maxLeft + maxRight) / 2
}
}
}
複製代碼
/** * @func * @name findMedian * @desc 使用二分法來獲取中位數 * @param {number} start1 第一個數組須要查找的起始位置 * @param {number[]} nums1 傳入的第一個數組 * @param {number} start2 第二個數組須要查找的起始位置 * @param {number[]} nums2 傳入的第二個數組 * @param {number} half nums1與nums2的長度中間值 * @return {number} nums1與nums2的中位數 */
const findMedian = (start1: number, nums1: number[], start2: number, nums2: number[], half: number): number => {
const [{ length: len1 }, { length: len2 }] = [nums1, nums2]
// 下面兩個if主要是對空數組的判斷,若是是有一個是空數組,則輸出另外一個數組的中位數
if (start1 >= len1) {
return nums2[start2 + half - 1]
}
if (start2 >= len2) {
return nums1[start1 + half - 1]
}
if (half === 1) { // 此時兩個數組總長度的一半就是等於1,說明兩個數組就只有一個值,那麼就哪一個小輸出哪一個
return Math.min(nums1[start1], nums2[start2])
}
// 下面的不變量定義主要是爲了查找是否有中間值,若是沒有就賦值爲Infinity,不然則賦值爲這個中間值
const halfVal1 = (start1 + parseInt(half / 2) - 1 < len1
? nums1[start1 + parseInt(half / 2) - 1]
: Infinity)
const halfVal2 = (start2 + parseInt(half / 2) - 1 < len2
? nums2[start2 + parseInt(half / 2) - 1]
: Infinity)
// 這裏是二分法的核心,判斷兩個數組中間值的大小,若是nums1的(half / 2)比較小的話,就說明咱們找的數字不在nums1的前(half / 2)數字之中,因此咱們nums1就日後移動(half / 2)。反之就是nums2日後移動(half / 2)。一直用二分法遞歸對比沒有判斷過的數,從而獲得結果值。
return (halfVal1 < halfVal2
? findMedian(start1 + parseInt(half / 2), nums1, start2, nums2, half - parseInt(half / 2))
: findMedian(start1, nums1, start2 + parseInt(half / 2), nums2, half - parseInt(half / 2)));
}
/** * @func * @name findMedianSortedArrays * @desc 尋找兩個有序數組的中位數 * @param {number[]} nums1 傳入的第一個數組 * @param {number[]} nums2 傳入的第二個數組 * @return {number} 中位數 */
const findMedianSortedArrays = (nums1: number, nums2: number): number => {
const [{ length: len1 }, { length: len2 }] = [nums1, nums2]
const totalLen = len1 + len2 // 兩個數組的總長度
return (totalLen % 2 === 1
? findMedian(0, nums1, 0, nums2, (totalLen + 1) / 2) // 總長度爲奇數時
: (findMedian(0, nums1, 0, nums2, totalLen / 2) + findMedian(0, nums1, 0, nums2, totalLen / 2 + 1)) / 2) // 總長度爲偶數時
}
/** * @func * @name findMedianSortedArrays2 * @desc 尋找兩個有序數組的中位數 * @param {number[]} nums1 傳入的第一個數組 * @param {number[]} nums2 傳入的第二個數組 * @return {number} 中位數 */
const findMedianSortedArrays2 = (nums1: number[], nums2: number[]): number => {
let len1: number = nums1.length
let len2: number = nums2.length
// 下面交換法主要是爲了統一一個方向,方便迭代
if (len1 > len2) {
let lenTemp: number = len1
let numsTemp: number[] = nums1
len1 = len2
len2 = lenTemp
nums1 = nums2
nums2 = numsTemp
}
let left: number = 0 // 左邊開始的位置
let right: number = len1 // 右邊開始的位置
let mid: number = Math.floor((len1 + len2 + 1) / 2) // 中間位置
// 二分法 迭代模式
while (left <= right) {
let i: number = Math.floor((left + right) / 2) // 左邊遍歷到的位置
let j: number = Math.floor(mid - i) // 右邊遍歷到的位置
if (i < len1 && nums2[j - 1] > nums1[i]) { // 若是當前左邊遍歷的位置還沒到數組1的長度,而且數組二遍歷到的元素是比數組已的元素大的,那麼這該時候就說明i還過小,須要+1
left = i + 1
} else if (i > 0 && nums1[i - 1] > nums2[j]) { // 如上,相反的邏輯 - 1
right = i - 1
} else {
let maxLeft: number
let maxRight: number
// 下面一層二層的判斷是爲了處理當有一個數組長度爲0時的狀況
if (i === 0) {
maxLeft = nums2[j - 1]
} else if (j === 0) {
maxLeft = nums1[i - 1]
} else { // 此時則是獲取當前遍歷到的兩個數組對比的最大值
maxLeft = Math.max(nums1[i - 1], nums2[j - 1])
}
if ((len1 + len2) % 2 === 1) { // 若是兩個數組的長度爲奇數,能夠直接輸出最大值
return maxLeft
}
// 下面則是判斷數組長度加起來爲偶數時的狀況
if (i === len1) { // 當i已經遍歷完時,則右邊中間值爲第二個數組當前遍歷的值
maxRight = nums2[j]
} else if (j === len2) { // 與上相反
maxRight = nums1[i]
} else { // 獲取當前遍歷到的值中的最大值
maxRight = Math.min(nums1[i], nums2[j])
}
// 最終的值爲left right兩邊中間值的一半
return (maxLeft + maxRight) / 2
}
}
}
複製代碼
from typing import List
def findMedian(start1: int, nums1: List[int], start2: int, nums2: List[int], half: int) -> float:
""" :type start1: int :type nums1: List[int] :type start2: int :type nums2: List[int] :type half: int :rtype: float """
len1 = len(nums1)
len2 = len(nums2)
if start1 >= len1:
return nums2[int(start2 + half - 1)]
if start2 >= len2:
return nums1[int(start1 + half - 1)]
if half == 1:
return min(nums1[start1], nums2[start2])
halfVal1 = float('Infinity')
halfVal2 = float('Infinity')
if (start1 + int(half / 2) - 1) < len1:
halfVal1 = nums1[start1 + int(half / 2) - 1]
if (start2 + int(half / 2) - 1) < len2:
halfVal2 = nums2[start2 + int(half / 2) - 1]
if halfVal1 < halfVal2:
return findMedian(start1 + int(half / 2), nums1, start2, nums2, half - int(half / 2))
else:
return findMedian(start1, nums1, start2 + int(half / 2), nums2, half - int(half / 2))
def findMedianSortedArrays(nums1: List[int], nums2: List[int]) -> float:
""" :type nums1: List[int] :type nums2: List[int] :rtype: float """
len1 = len(nums1)
len2 = len(nums2)
totalLen = len1 + len2
if totalLen % 2 == 1:
return findMedian(0, nums1, 0, nums2, (totalLen + 1) / 2)
else:
return (findMedian(0, nums1, 0, nums2, totalLen / 2) + findMedian(0, nums1, 0, nums2, totalLen / 2 + 1)) / 2
複製代碼
若是你喜歡探討技術,或者對本文有任何的意見或建議,很是歡迎加魚頭微信好友一塊兒探討,固然,魚頭也很是但願能跟你一塊兒聊生活,聊愛好,談天說地。 魚頭的微信號是:krisChans95 也能夠掃碼添加好友,備註「掘金」就行 ui