码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 4 Median of Two Sorted Arrays

时间:2015-01-07 22:00:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

对于两个数组的折半查找

public class Solution {
    public double findMedianSortedArrays(int A[], int B[]) {
        int m = A.length, n = B.length;

        if (n == 0) {
            int mid = (m + 1) / 2;
            if (m % 2 == 0) {
                return (A[mid - 1] + A[mid]) / 2.0;
            } else {
                return A[mid - 1];
            }
        }

        if (m == 0) {
            return findMedianSortedArrays(B, A);
        }

        if (m > 1) {
            if (A[1] < A[0]) {
                int temp = 0;
                for (int i = 0; i < m / 2; i++) {
                    temp = A[i];
                    A[i] = A[m - 1 - i];
                    A[m - 1 - i] = temp;
                }
            }
        }

        if (n > 1) {
            if (B[1] < B[0]) {
                int temp = 0;
                for (int i = 0; i < n / 2; i++) {
                    temp = B[i];
                    B[i] = B[n - 1 - i];
                    B[n - 1 - i] = temp;
                }
            }
        }

        if (A[m - 1] <= B[0]) {
            int mid = (m + n + 1) / 2;
            if ((m + n) % 2 == 0) {
                if (mid > m) {
                    return (B[mid - m - 1] + B[mid - m]) / 2.0;
                } else if (mid == m) {
                    return (A[m - 1] + B[0]) / 2.0;
                } else if (mid < m) {
                    return (A[mid - 1] + A[mid]) / 2.0;
                }
            } else {
                if (mid <= m) {
                    return A[mid - 1];
                } else {
                    return B[mid - m - 1];
                }
            }
        }

        if (B[n - 1] <= A[0]) {
            return findMedianSortedArrays(B, A);
        }

        if (m > n) {
            return findMedianSortedArrays(B, A);
        }

        int length = (m + n) / 2 + 1;

        int la = 0, ra = m - 1;
        int ma = (la + ra) / 2, mb = length - (ma + 1) - 1;
        int pma = -1, pmb = -1;
        double med = 0;
        while (true) {
            if (A[ma] <= B[mb]) {
                if (ma < ra) {
                    la = ma + 1;
                } else {
                    break;
                }

                pma = ma;
                pmb = mb;
                ma = (la + ra) / 2;
                mb = length - (ma + 1) - 1;
            } else {
                if (ma > la) {
                    ra = ma - 1;
                } else {
                    break;
                }

                pma = ma;
                pmb = mb;
                ma = (ma + ra) / 2;
                mb = length - (ma + 1) - 1;
            }

        }

        if (pma > -1 && pmb > -1) {
            int[] rlts = { A[ma], B[mb], A[pma], B[pmb] };

            Arrays.sort(rlts);

            if ((m + n) % 2 == 0) {
                med = (rlts[1] + rlts[2]) / 2.0;
            } else {
                med = rlts[2];
            }

        } else {
            int[] rlts = { A[ma], B[mb]};

            Arrays.sort(rlts);

            if ((m + n) % 2 == 0) {
                med = (rlts[1] + rlts[0]) / 2.0;
            } else {
                med = rlts[1];
            }
        }
        return med;
    }
}

技术分享

leetcode 4 Median of Two Sorted Arrays

标签:

原文地址:http://www.cnblogs.com/aDreamer/p/4209424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!