1. 程式人生 > >兩個有序陣列的中位數Median of Two Sorted Arrays(很重要)

兩個有序陣列的中位數Median of Two Sorted Arrays(很重要)

https://leetcode.com/problems/median-of-two-sorted-arrays/

對於一個長度為n的已排序數列a,若n為奇數,中位數為a[n / 2 + 1] , 
    若n為偶數,則中位數(a[n / 2] + a[n / 2 + 1]) / 2
    如果我們可以在兩個數列中求出第K小的元素,便可以解決該問題
    不妨設數列A元素個數為n,數列B元素個數為m,各自升序排序,求第k小元素
    取A[k / 2] B[k / 2] 比較,
    如果 A[k / 2] > B[k / 2] 那麼,所求的元素必然不在B的前k / 2個元素中(證明反證法)
    反之,必然不在A的前k / 2個元素中,於是我們可以將A或B數列的前k / 2元素刪去,求剩下兩個數列的
    k - k / 2小元素,於是得到了資料規模變小的同類問題,遞迴解決
    如果 k / 2 大於某數列個數,所求元素必然不在另一數列的前k / 2個元素中,同上操作就好。
class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        // write your code here
        int len1=nums1.size();
        int len2=nums2.size();
        int len=len1+len2;
        if(len & 1){
            return findKth(nums1,0,nums2,0,len/2+1);
        }
        return (findKth(nums1,0,nums2,0,len/2)+findKth(nums1,0,nums2,0,len/2+1))/2;
    }

    double findKth(vector<int>& nums1,int i1,vector<int>& nums2,int i2,int k){
        if(i1>=nums1.size()){
            return nums2[i2+k-1];
        }
        if(i2>=nums2.size()){
            return nums1[i1+k-1];
        }
        if(k==1){
            return min(nums1[i1],nums2[i2]);
        }

        int key1=i1+k/2-1>=nums1.size()?INT_MAX:nums1[i1+k/2-1];
        int key2=i2+k/2-1>=nums2.size()?INT_MAX:nums2[i2+k/2-1];
        if(key1<key2){
            return findKth(nums1,i1+k/2,nums2,i2,k-k/2);
        }else{
            return findKth(nums1,i1,nums2,i2+k/2,k-k/2);
        }
    }

};

參考http://www.jiuzhang.com/solutions/median-of-two-sorted-arrays/

相關推薦

有序陣列位數Median of Two Sorted Arrays重要

https://leetcode.com/problems/median-of-two-sorted-arrays/ 對於一個長度為n的已排序數列a,若n為奇數,中位數為a[n / 2 + 1]

有序陣列位數 Median of Two Sorted Arrays

Leetcode上的一道題: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall ru

4. 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 ru

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 tim

LeetCode | Median of Two Sorted Arrays陣列位數

題目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time com

4. Median of Two Sorted Arrays排序陣列位數

有兩個大小為 m 和 n 的排序陣列 nums1 和 nums2 。 請找出兩個排序陣列的中位數並且總的執行時間複雜度為 O(log (m+n)) 。 示例 1: nums1 = [1, 3] n

有序陣列位數

大小m和n分別有兩個排序陣列A和B。找到兩個排序陣列的中值。總的執行時間複雜度應該是O(log(m+n))。class Solution {  public:      /**      * @param A: An integer array.      * @param 

【LeetCode 4】Median of Two Sorted ArraysPython)

Problem: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median

Leetcode 4. Median of Two Sorted Arrays二分

4. Median of Two Sorted Arrays 題目連結:https://leetcode.com/problems/median-of-two-sorted-arrays/ Description: There are two sorted arrays nums1&

【死磕演算法之1刷Leetcode】——找出有序陣列位數Median of Two Sorted Arrays】O(log(m+n))

Median of Two Sorted Arrays 題目難度:hard 題目要求: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two s

[LeetCode] 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 shou

Leetcode 4. Median of Two Sorted Arrays 尋找有序陣列位數

Leetcode 4. Median of Two Sorted Arrays 尋找兩個有序陣列的中位數 標籤: Leetcode 題目地址:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 題目

【LeetCode 4. Median of Two Sorted Arrays有序陣列位數求解

一、題目描述 給定兩個已經排好序的陣列nums1和nums2,長度分別是m和n,要求求出這兩個有序數組合並後的新陣列中的中位數,並要求整個程式實現的時間複雜度為O(log(m+n))。例如陣列1 nums1=[1,3],給定的陣列2為 nums2=[2],則輸出陣列[1,2

【LeetCode】4.Median of Two Sorted Arrays 排序陣列位數

示例 1: nums1 = [1, 3] nums2 = [2] 中位數是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位數是 (2 + 3)/2 = 2.5 解題思路: 糟糕- -沒理解題意,首先需要知道“中位數”

leetcode 排序陣列位數Median of Two Sorted Arrays

解決方案 方法:遞迴法 為了解決這個問題,我們需要理解“中位數的作用是什麼”。在統計中,中位數被用來: 將一個集合劃分為兩個長度相等的子集,其中一個子集中的元素總是大於另一個子集中的元素。 如果理解了中位數的劃分作用,我們就很接近答案了。 首先,讓我們在任一位置

LeetCode-4: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 overa

4.、Median of Two Sorted Arrays(有序序列的位數)

題目要求在給定兩個長度分別為m和n的有序序列時,找出這兩個序列合起後的中位數。並且要求時間複雜度為O(log(m+n))。首先,中位數的定義是,當序列長度為偶數時,中位數的值為序列中間兩個數的均值;當序列為奇數時,中位數為正中間的數。當序列長度為n時,其值分別為(array[

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

[LeetCode]Median of Two Sorted Arrays 二分查找有序數組的第k數位數

大於 data div ble 關系 操作 spa 兩個 -1 二分。情況討論 因為數組有序,所以能夠考慮用二分。通過二分剔除掉肯定不是第k位數的區間。如果數組A和B當前處理的下標各自是mid1和mid2。則 1、假設A[mid1]<B[mid2], ①

有序數組的位數4. Median of Two Sorted Arrays

排序 font float 序列 大小 width 技術 display 個數 先吐槽一下,我好氣啊,想了很久硬是沒有做出來,題目要求的時間復雜度為O(log(m+n)),我猜到了要用二分法,但是沒有想到點子上去。然後上網搜了一下答案,感覺好有罪惡感。 題目原型 正確的