首页 > 其他 > 详细

4. Median of Two Sorted Arrays (Solution 1: Merge Sort)

时间:2020-07-29 02:10:41      阅读:71      评论:0      收藏:0      [点我收藏+]
package LeetCode_4

/**
 * 4. Median of Two Sorted Arrays
 * https://leetcode.com/problems/median-of-two-sorted-arrays/description/
 *
 * 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)).
You may assume nums1 and nums2 cannot be both empty.

Example 1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0

Example 2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
 * */
class Solution {
    /*
    * solution 1: Merge Sort, Time complexity:O(m+n), Space complexity:O(m+n)
    * solution 2: Binary Search,
    * */
    fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
        val mergedArray = mergeArray(nums1, nums2)
        val size = mergedArray.size
        var result = 0.0
        if (size % 2 == 0) {
            //even
            result = (mergedArray[size/2] + mergedArray[(size-1)/2])/2.0
        } else {
            //odd
            result = mergedArray[size/2].toDouble()
        }
        return result
    }

    private fun mergeArray(nums1: IntArray, nums2: IntArray): IntArray {
        val n1 = nums1.size
        val n2 = nums2.size
        val newArray = IntArray(n1 + n2)
        var k = 0
        var i = 0
        var j = 0
        while (i < n1 && j < n2) {
            //compare two value
            if (nums1[i] <= nums2[j]) {
                newArray[k] = nums1[i]
                i++
            } else {
                newArray[k] = nums2[j]
                j++
            }
            k++
        }
        //check remaining element in nums1
        while (i < n1) {
            newArray[k] = nums1[i]
            i++
            k++
        }
        //check remaining element in nums2
        while (j < n2) {
            newArray[k] = nums2[j]
            j++
            k++
        }
        return newArray
    }
}

 

4. Median of Two Sorted Arrays (Solution 1: Merge Sort)

原文:https://www.cnblogs.com/johnnyzhao/p/13394642.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!