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 run time complexity should be O(log (m+n)).

提交超时,偶尔一次提交能通过。

public class No4 {

    public static void main(String[] args) {

        int[] arr1 = { 16, 47, 230, 439, 717, 730, 921, 939, 1075, 1174, 1240,
                1587, 1716, 1738, 1757, 1835, 1896, 1899, 1917, 1957, 1962,
                1994, 2044, 2203, 2276, 2351, 2388, 2404, 2460, 2473, 2580,
                2650, 2672, 2738, 2800, 2809, 3038, 3100, 3102, 3139, 3142,
                3182, 3292, 3417, 3516, 3573, 3638, 3770, 3876, 4049, 4103,
                4121, 4125, 4135, 4140, 4300, 4335, 4558, 4582, 4915, 5033,
                5072, 5154, 5168, 5271, 5485, 5609, 5631, 5728, 5755, 5800,
                5873, 5903, 6012, 6088, 6287, 6316, 6468, 6493, 6504, 6513,
                6925, 6944, 6954, 6997, 7004, 7048, 7077, 7313, 7346, 7381,
                7401, 7438, 7593, 7639, 7753, 7763, 7794, 7841, 7869, 7874,
                7881, 7938, 8134, 8181, 8235, 8364, 8478, 8540, 8566, 8657,
                8663, 8833, 8973, 8990, 9084, 9189, 9221, 9224, 9591, 9672,
                9722, 9768, 9832, 9974, 10067, 10070, 10072, 10187, 10274,
                10372, 10668, 10852, 11076, 11144, 11192, 11220, 11431, 11469,
                11596, 11644, 11649, 11701, 11724, 11815, 11856, 12054, 12117,
                12131, 12208, 12316, 12326, 12357, 12474, 12572, 12608, 12668,
                12721, 12943, 12980, 13181, 13265, 13534, 13562, 13620, 13646,
                13778, 13783, 13866, 13950, 13984, 14243, 14343, 14359, 14427,
                14662, 14727, 14790, 14790, 15014, 15070, 15276, 15377, 15392,
                15448, 15453, 15576, 15622, 15629, 15676, 15920, 15928, 16041,
                16499, 16576, 16676, 16854, 17025, 17136, 17289, 17532, 17564,
                17593, 17621, 17745, 17834, 18193, 18237, 18314, 18368, 18488,
                18594, 18737, 18822, 18823, 18920, 18947, 19275, 19340, 19391,
                19450, 19605, 19922, 20272, 20356, 20459, 20537, 20682, 20792,
                20864, 20913, 21001, 21063, 21167, 21212, 21225, 21230, 21333,
                21520, 21713, 21723, 22159, 22183, 22471, 22575, 22637, 22845,
                23040, 23074, 23079, 23161, 23195, 23344, 23351, 23391, 23408,
                23522, 23529, 23534, 23542, 23588, 23945, 23980, 24107, 24132,
                24184, 24239, 24384, 24422, 24472, 24506, 24556, 24570, 24626,
                24834, 24914, 25032, 25056, 25076, 25098, 25106, 25180, 25255,
                25277, 25511, 25521, 25524, 25626, 25778, 26150, 26203, 26291,
                26518, 26577, 26678, 26922, 26945, 26957, 27064, 27142, 27203,
                27222, 27368, 27588, 27625, 27634, 27682, 28052, 28132, 28210,
                28420, 28494, 28496, 28513, 28620, 28686, 28705, 28726, 28871,
                28876, 28897, 29449, 29460, 29463, 29533, 29579, 29657, 29771,
                29801, 29814, 29877, 29916, 29924, 29993, 30074, 30085, 30110,
                30199, 30222, 30245, 30444, 30513, 30525, 30729, 30886, 30925,
                31018, 31095, 31254, 31401, 31416, 31549, 31597, 31810, 31857,
                32086, 32148, 32588 };
        int[] arr2 = { 17, 46, 151, 181, 197, 229, 262, 286, 308, 493, 562,
                575, 636, 657, 715, 833, 900, 913, 963, 1042, 1079, 1176, 1346,
                1412, 1423, 1497, 1534, 1571, 1615, 1622, 1630, 1674, 1692,
                1831, 1854, 1981, 2026, 2042, 2052, 2073, 2086, 2090, 2101,
                2139, 2177, 2185, 2204, 2209, 2314, 2382, 2385, 2424, 2555,
                2700, 2716, 2867, 2881, 2886, 2890, 2900, 2963, 3066, 3098,
                3107, 3124, 3132, 3187, 3196, 3217, 3217, 3318, 3362, 3375,
                3395, 3443, 3443, 3500, 3557, 3588, 3599, 3644, 3656, 3660,
                3830, 3834, 3841, 3870, 3870, 3871, 3900, 4050, 4389, 4428,
                4481, 4549, 4571, 4596, 4756, 4817, 5070, 5112, 5163, 5166,
                5215, 5287, 5330, 5360, 5370, 5383, 5426, 5481, 5517, 5523,
                5613, 5694, 5891, 5891, 5893, 6035, 6062, 6110, 6162, 6164,
                6171, 6224, 6228, 6334, 6365, 6375, 6390, 6396, 6398, 6441,
                6452, 6614, 6623, 6722, 6799, 6905, 6927, 6940, 6996, 7052,
                7175, 7268, 7287, 7304, 7342, 7391, 7426, 7474, 7562, 7566,
                7612, 7639, 7756, 7854, 7862, 7912, 7972, 8001, 8142, 8188,
                8191, 8205, 8235, 8300, 8688, 8730, 8747, 8755, 8773, 8799,
                8849, 8975, 8989, 9011, 9106, 9229, 9256, 9335, 9389, 9415,
                9424, 9538, 9565, 9616, 9623, 9656, 9675, 9714, 9714, 9725,
                9733, 9805, 9848, 9875, 9908, 9913, 9939, 10072, 10084, 10110,
                10132, 10161, 10479, 10578, 10594, 10604, 10610, 10717, 10785,
                10827, 10827, 10853, 10898, 10905, 10919, 10934, 10951, 10960,
                10961, 11070, 11148, 11160, 11291, 11357, 11386, 11399, 11424,
                11452, 11648, 11734, 11805, 11874, 11893, 11899, 11925, 11930,
                12009, 12037, 12048, 12054, 12057, 12079, 12081, 12321, 12458,
                12504, 12741, 12815, 12815, 12846, 12974, 12985, 13070, 13129,
                13148, 13150, 13205, 13319, 13630, 13853, 13861, 13921, 13960,
                14005, 14011, 14149, 14151, 14177, 14220, 14248, 14346, 14452,
                14457, 14498, 14500, 14672, 14686, 14729, 14742, 14749, 14759,
                14777, 14789, 14802, 14859, 14861, 14861, 14999, 15003, 15067,
                15069, 15167, 15198, 15220, 15286, 15389, 15432, 15438, 15444,
                15511, 15512, 15514, 15515, 15555, 15570, 15680, 15702, 15719,
                15722, 15723, 15764, 15781, 15850, 15884, 15887, 15943, 16043,
                16060, 16110, 16136, 16158, 16219, 16273, 16287, 16294, 16328,
                16384, 16408, 16478, 16488, 16504, 16546, 16566, 16605, 16710,
                16712, 16762, 16867, 16965, 17004, 17245, 17247, 17314, 17433,
                17434, 17453, 17519, 17569, 17583, 17626, 17700, 17711, 17777,
                17819, 17876, 17931, 17972, 18025, 18034, 18183, 18206, 18221,
                18223, 18314, 18347, 18349, 18416, 18425, 18472, 18498, 18645,
                18664, 18767, 18792, 19000, 19084, 19131, 19157, 19162, 19226,
                19314, 19429, 19518, 19558, 19622, 19649, 19728, 19744, 19868,
                19879, 19957, 19959, 20049, 20151, 20239, 20239, 20329, 20346,
                20395, 20493, 20504, 20512, 20553, 20695, 20756, 20790, 20856,
                20890, 20900, 20928, 21046, 21171, 21216, 21443, 21443, 21471,
                21526, 21553, 21859, 21894, 21950, 21961, 22095, 22113, 22127,
                22185, 22210, 22225, 22329, 22359, 22361, 22424, 22505, 22528,
                22605, 22640, 22719, 22792, 22818, 22823, 22850, 22855, 22903,
                23076, 23197, 23213, 23237, 23245, 23259, 23444, 23485, 23550,
                23574, 23671, 23718, 23817, 23826, 23840, 23924, 24019, 24054,
                24183, 24191, 24276, 24303, 24655, 24690, 24786, 24807, 24817,
                24891, 24914, 25025, 25085, 25123, 25219, 25248, 25327, 25351,
                25353, 25366, 25380, 25419, 25438, 25487, 25488, 25544, 25593,
                25768, 25792, 25806, 25817, 25822, 25828, 25858, 25950, 25954,
                25972, 25996, 25997, 26019, 26049, 26095, 26293, 26298, 26300,
                26421, 26438, 26440, 26442, 26503, 26510, 26532, 26615, 26628,
                26643, 26648, 26702, 26750, 26797, 26833, 26859, 26860, 26919,
                26935, 26997, 27096, 27140, 27169, 27169, 27206, 27240, 27249,
                27250, 27256, 27294, 27331, 27367, 27375, 27409, 27411, 27443,
                27509, 27517, 27532, 27584, 27594, 27599, 27646, 27708, 27738,
                27945, 28004, 28016, 28121, 28128, 28135, 28136, 28159, 28245,
                28352, 28405, 28454, 28520, 28548, 28570, 28606, 28749, 28766,
                28770, 28781, 28815, 28881, 29132, 29221, 29250, 29311, 29313,
                29332, 29336, 29344, 29345, 29356, 29390, 29441, 29543, 29568,
                29600, 29613, 29635, 29642, 29710, 29746, 29767, 29768, 29797,
                29804, 29820, 29888, 29916, 29925, 29933, 29974, 29990, 30008,
                30011, 30026, 30163, 30264, 30274, 30301, 30342, 30342, 30369,
                30480, 30541, 30590, 30615, 30727, 30731, 30868, 30871, 30910,
                30924, 30948, 30993, 31014, 31031, 31050, 31107, 31189, 31276,
                31301, 31307, 31451, 31587, 31794, 31806, 31824, 31847, 31958,
                31976, 32252, 32263, 32328, 32330, 32342, 32438, 32674, 32763 };

        System.out.println(findMedianSortedArrays(arr1, arr2));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

        int m = nums1.length;
        int n = nums2.length;
        int[] sum = new int[m + n];

        if (m == 0 && n == 0) {
            return 0;
        } else if (m == 0) {
            sum = nums2;
        } else if (n == 0) {
            sum = nums1;
        } else {
            int i = 0, j = 0, k = 0;
            while (i < m && j < n && k < ((m + n) / 2 + 2)) {
                if (nums1[i] <= nums2[j]) {
                    sum[k] = nums1[i];
                    i++;
                    k++;
                } else {
                    sum[k] = nums2[j];
                    j++;
                    k++;
                }
            }
            while (j < n && k < ((m + n) / 2 + 2)) {
                sum[k] = nums2[j];
                j++;
                k++;
            }
            while (i < m && k < ((m + n) / 2 + 2)) {
                sum[k] = nums1[i];
                i++;
                k++;
            }
        }

//      for (int i = 0; i < sum.length; i++) {
//          System.out.println(sum[i]);
//      }
        System.out.println(sum.length);

        if (sum.length % 2 == 0) { // sum有双数个的情况
            return (double) (sum[sum.length / 2] + sum[sum.length / 2 - 1]) / 2;
        } else {
            return (double) (sum[sum.length / 2]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值