国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

給定兩個大小分別為 m 和 n 的正序(從小到大)數(shù)組 nums1 和 nums2。請你找出并返回

2023-02-28 10:17 作者:酸奶公園  | 我要投稿


給定兩個大小分別為 m 和 n 的正序(從小到大)數(shù)組 nums1 和 nums2。請你找出并返回這兩個正序數(shù)組的 中位數(shù) 。


算法的時間復雜度應該為 O(log (m+n)) 。


?


示例 1:


輸入:nums1 = [1,3], nums2 = [2]

輸出:2.00000

解釋:合并數(shù)組 = [1,2,3] ,中位數(shù) 2

示例 2:


輸入:nums1 = [1,2], nums2 = [3,4]

輸出:2.50000

解釋:合并數(shù)組 = [1,2,3,4] ,中位數(shù) (2 + 3) / 2 = 2.5

?


?


提示:


nums1.length == m

nums2.length == n

0 <= m <= 1000

0 <= n <= 1000

1 <= m + n <= 2000

-106 <= nums1[i], nums2[i] <= 106


class Solution {

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

? ? ? ? int n1 = nums1.length;

? ? ? ? int n2 = nums2.length;

? ??

? ? ? ? if (n1 > n2) return findMedianSortedArrays(nums2, nums1);

? ??

? ? ? ? int iMin = 0;

? ? ? ? int iMax = n1;

? ? ? ? int mid = (n1 + n2 + 1) / 2;

? ? ? ? double median;

? ??

? ? ? ? while (iMin <= iMax) {

? ? ? ? ? ? int i = (iMin + iMax) / 2;

? ? ? ? ? ? int j = mid - i;?

? ? ? ? ? ? if (i < iMax && nums2[j - 1] > nums1[i]) {

? ? ? ? ? ? ? ? iMin++;

? ? ? ? ? ? }

? ? ? ? ? ? else if (i > iMin && nums1[i - 1] > nums2[j]) {

? ? ? ? ? ? ? ? iMax--;?

? ? ? ? ? ? }

? ? ? ? ? ? else {

? ? ? ? ? ? ? ? int maxLeft;?

? ? ? ? ? ? ? ? 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 ((n1 + n2) % 2 == 1) {

? ? ? ? ? ? ? ? ? ? return maxLeft;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? int minRight;

? ? ? ? ? ? ? ? if (i == n1)

? ? ? ? ? ? ? ? ? ? minRight = nums2[j];

? ? ? ? ? ? ? ? else if (j == n2)

? ? ? ? ? ? ? ? ? ? minRight = nums1[i];

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? minRight = Math.min(nums1[i], nums2[j]);

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? median = (double) (maxLeft + minRight) / 2.0;

? ? ? ? ? ? ? ? return median;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return 0.0;

? ? }

}


給定兩個大小分別為 m 和 n 的正序(從小到大)數(shù)組 nums1 和 nums2。請你找出并返回的評論 (共 條)

分享到微博請遵守國家法律
高平市| 静乐县| 黎平县| 元江| 铁岭县| 华容县| 图们市| 和静县| 如皋市| 沂水县| 岳阳市| 泌阳县| 邵阳市| 随州市| 聊城市| 曲阜市| 醴陵市| 阿坝| 广河县| 台北县| 景谷| 扶风县| 嘉禾县| 股票| 定西市| 安岳县| 庐江县| 呼伦贝尔市| 同仁县| 封开县| 金昌市| 西华县| 大石桥市| 桂阳县| 左贡县| 威海市| 酉阳| 日照市| 年辖:市辖区| 三门峡市| 尚义县|