[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python),leetcodemedian

來源:互聯網
上載者:User

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python),leetcodemedian

索引:[LeetCode] Leetcode 題解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode


004.Median_of_Two_Sorted_Arrays (Hard)連結:

題目:https://oj.leetcode.com/problems/Median-of-Two-Sorted-Arrays/
代碼(github):https://github.com/illuz/leetcode

題意:

求兩個有序數組的中位元。要求複雜度是 O(log(n + m))。

分析:

兩種思路:
1. 直接 merge 兩個數組,然後求中位元,能過,不過複雜度是 O(n + m)。
2. 用二分的思路去做,這不好想,還要考慮到奇偶。可以轉化思維,去求兩個有序數組中的第 K 大數,這樣就比較好想了。

代碼:

1. Merge

C++:

class Solution {public:double findMedianSortedArrays(int A[], int m, int B[], int n) {vector<int> C;int pa = 0, pb = 0;// point of A & Bwhile (pa < m || pb < n) {if (pa == m) {C.push_back(B[pb++]);continue;}if (pb == n) {C.push_back(A[pa++]);continue;}if (A[pa] > B[pb])C.push_back(B[pb++]);elseC.push_back(A[pa++]);}if ((n + m)&1)return C[(n+m)/2];elsereturn (C[(n+m)/2 - 1] + C[(n+m)/2]) / 2.0;}};


2. 二分

C++:

class Solution {private:double findKthSortedArrays(int A[], int m, int B[], int n, int k) {if (m < n) {swap(n, m);swap(A, B);}if (n == 0)return A[k - 1];if (k == 1)return min(A[0], B[0]);int pb = min(k / 2, n), pa = k - pb;if (A[pa - 1] > B[pb - 1])return findKthSortedArrays(A, m, B + pb, n - pb, k - pb);else if (A[pa - 1] < B[pb - 1])return findKthSortedArrays(A + pa, m - pa, B, n, k - pa);elsereturn A[pa - 1];}public:double findMedianSortedArrays(int A[], int m, int B[], int n) {if ((n + m)&1)return findKthSortedArrays(A, m, B, n, (n + m) / 2 + 1);elsereturn (findKthSortedArrays(A, m, B, n, (n + m) / 2 + 1) +findKthSortedArrays(A, m, B, n, (n + m) / 2)) / 2.0;}};


Java:

public class Solution {    private double findKthSortedArrays(int A[], int astart, int aend,                                       int B[], int bstart, int bend, int k) {        int m = aend - astart, n = bend - bstart;        if (m < n) {            return findKthSortedArrays(B, bstart, bend, A, astart, aend, k);        }        if (n == 0)            return A[astart + k - 1];        if (k == 1)            return Math.min(A[astart], B[bstart]);        int pb = Math.min(k / 2, n), pa = k - pb;        if (A[astart + pa - 1] > B[bstart + pb - 1])            return findKthSortedArrays(A, astart, aend, B, bstart + pb, bend, k - pb);        else if (A[astart + pa - 1] < B[bstart + pb - 1])            return findKthSortedArrays(A, astart + pa, aend, B, bstart, bend, k - pa);        else            return A[astart + pa - 1];    }    public double findMedianSortedArrays(int A[], int B[]) {        int m = A.length, n = B.length;        if ((n + m) % 2 == 1)            return findKthSortedArrays(A, 0, m, B, 0, n, (n + m) / 2 + 1);        else            return (findKthSortedArrays(A, 0, m, B, 0, n, (n + m) / 2 + 1) +                    findKthSortedArrays(A, 0, m, B, 0, n, (n + m) / 2)) / 2.0;    }}


Python:

class Solution:    def findKthSortedArrays(self, A, B, k):        if len(A) < len(B):            tmp = A            A = B            B = tmp        if len(B) == 0:            return A[k - 1]        if k == 1:            return min(A[0], B[0])        pb = min(k / 2, len(B))        pa = k - pb        if A[pa - 1] > B[pb - 1]:            return self.findKthSortedArrays(A, B[pb:], k - pb)        elif A[pa - 1] < B[pb - 1]:            return self.findKthSortedArrays(A[pa:], B, k - pa)        else:            return A[pa - 1]    # @return a float    def findMedianSortedArrays(self, A, B):        if (len(A) + len(B)) % 2 == 1:            return self.findKthSortedArrays(A, B, (len(A) + len(B)) / 2 + 1)        else:            return (self.findKthSortedArrays(A, B, (len(A) + len(B)) / 2) +                self.findKthSortedArrays(A, B, (len(A) + len(B)) / 2 + 1)) / 2.0



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.