题目Median of Two Sorted Arrays难度Hard

方案1,数组合并&排序调用Java方法

 import java.util.*

 class Solution {
     fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
         val lenNums1 = nums1.size
         val lenNums2 = nums2.size

         val array = Arrays.copyOf(nums1, lenNums1 + lenNums2)
         System.arraycopy(nums2, , array, lenNums1, lenNums2)

         Arrays.sort(array)

         var median: Double
         val lenArray = array.size
          == ) {
             median = array[(lenArray - ) / ].toDouble()
         } else {
             median = (array[(lenArray - ) / ] + array[lenArray / ]).toDouble() /
         }

         return median
     }
 }

提交详情1

平均耗时0.25ms。

方案2,数组合并&排序调用Kotlin方法

 class Solution {
     fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
         val lenNums1 = nums1.size
         val lenNums2 = nums2.size

         val array = IntArray(lenNums1 + lenNums2)
         System.arraycopy(nums1, , array, , lenNums1)
         System.arraycopy(nums2, , array, lenNums1, lenNums2)

         array.sort()

         var median: Double
         val lenArray = array.size
          == ) {
             median = array[(lenArray - ) / ].toDouble()
         } else {
             median = (array[(lenArray - ) / ] + array[lenArray / ]).toDouble() /
         }

         return median
     }
 }

提交详情2

平均耗时0.27ms。

Java & Kotlin代码对比

其实,通过源码可以发现,方案1和2在对数组进行合并与排序时调用的方法是一样的。

Arrays.java

 public static int[] copyOf(int[] original, int newLength) {
     int[] copy = new int[newLength];
     System.arraycopy(original, , copy, ,
                      Math.min(original.length, newLength));
     return copy;
 }

copyOf方法内部调用的还是System的静态方法arraycopy(native就不往下追了)。

System.java

 public static native void arraycopy(Object src,  int  srcPos,
                                         Object dest, int destPos,
                                         int length);

Arrays.kt

 /**
  * Creates a new array of the specified [size], where each element is calculated by calling the specified
  * [init] function. The [init] function returns an array element given its index.
  */
 public inline constructor(size: Int, init: (Int) -> Int)

IntArray(size: Int)会生成一个大小为size,元素值由init方法利用下标值计算而来,如果init不传入,那么默认均为0。

Arrays.kt

 public fun IntArray.sort(): Unit {
     ) java.util.Arrays.sort(this)
 }

Kotlin中IntArray的扩展方法sort,内部调用的是Java中Arrays的sort方法。

Arrays.java

 public static void sort(int[] a) {
     DualPivotQuicksort.sort(a, , a.length - , , );
 }

Arrays的sort方法最终是通过快排来实现的。而快速排序的时间复杂度为O(nlog(n)),但是题目要求量级为O(log(m+n))。

方案3,分治法求中位数

 class Solution {
     fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
         var media1: Int

         val len1 = nums1.size
         val len2 = nums2.size

          == ) {
             media1 = getMedian(nums1, nums2, , len1 - , , len2 - , (len1 + len2) /  + )
             return media1 / 1.0
         } else {
             media1 = getMedian(nums1, nums2, , len1 - , , len2 - , (len1 + len2) / )
             media2 = getMedian(nums1, nums2, , len1 - , , len2 - , (len1 + len2) /  + )
             return (media1 + media2) / 2.0
         }
     }

     fun getMedian(nums1: IntArray, nums2: IntArray, s1: Int, n1: Int, s2: Int, n2: Int, k: Int): Int {
         val x = (s1 + n1) /
         val y = (s2 + n2) / 

         if (s1 > n1)
             ]

         if (s2 > n2)
             ]

         return if (nums1[x] <= nums2[y]) {
             ) {
                 getMedian(nums1, nums2, s1, n1, s2, y - , k)
             } else {
                 getMedian(nums1, nums2, x + , n1, s2, n2, k - (x - s1) - )
             }
         } else {
             ) {
                 getMedian(nums1, nums2, s1, x - , s2, n2, k)
             } else {
                 getMedian(nums1, nums2, s1, n1, y + , n2, k - (y - s2) - )
             }
         }
     }
 }

提交详情3

平均耗时0.32ms。

结果分析

但从LeetCode的测试用例所消耗的时间来看,上述三种方案没有明显的区别,理论上分治法的时间复杂度为O(log(n))。

Kotlin实现LeetCode算法题之Median of Two Sorted Arrays的更多相关文章

  1. 算法题之Median of Two Sorted Arrays

    这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低... 题目: There are two sorted arrays nums1  ...

  2. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  3. 刷题4. Median of Two Sorted Arrays

    一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题 ...

  4. Kotlin实现LeetCode算法题之Two Sum

    LeetCode介绍 LeetCode是算法练习.交流等多功能网站,感兴趣的同学可以关注下(老司机请超车).页面顶部的Problems菜单对应算法题库,附带历史通过滤.难易程度等信息. 未来计划 打算 ...

  5. 【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively.  Find the median of the tw ...

  6. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  7. LeetCode(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 ...

  8. LeetCode解题笔记 - 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 ...

  9. LeetCode 笔记系列一 Median of Two Sorted Arrays

    题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sort ...

随机推荐

  1. KMP算法的细节问题

    preface: 想必,很多人都知道D.E.Knuth与V.R.Pratt和J.H.Morris同时提出所谓的狂拽酷炫屌炸天的KMP算法,在对字符串的匹配(或是字符串的查找)方面表现出比较好的效率,该 ...

  2. 初学者易上手的SSH-struts2 02Action获取表单数据-通配符

    在上一章中,我们已经搭建好了struts2的一个开发环境,那么这一章就来做一个简单的登录功能,以及介绍和使用struts2里面一个重要的东西-通配符. 第一步,在WebContent下面新建一个log ...

  3. VS2015智能提示由英文改为中文

    使用 VS2015 时,在 4.0 下智能提示显示中文,在 4.5 下显示英文,对于我这种爱(ying)国(yu)人(tai)士(lan)来说,用起来太不方便了,于 是在 知乎 上找到个好方法如下: ...

  4. 近期面试总结(PHP后端开发工程师)(部分笔试题)

    1.字符串"0"在PHP和js中转换为布尔值是false还是true php:false; php 弱语言 '0'和0一样: js:true:字符串除了空字符串('')其他都是tr ...

  5. win10 uwp 关联文件

    有时候应用需要打开后缀名为x的文件,那么如何从文件打开应用? 首先,需要打开 Package.appxmanifest 添加一个功能,需要添加最少有名称,文件类型. 上面的图就是我添加jpg 的方法, ...

  6. win10 UWP 标签

    本文主要翻译:http://visuallylocated.com/post/2015/02/20/Creating-a-WrapPanel-for-your-Windows-Runtime-apps ...

  7. MySQL自定义函数用法详解-复合结构自定义变量/流程控制

    自定义函数 (user-defined function UDF)就是用一个象ABS() 或 CONCAT()这样的固有(内建)函数一样作用的新函数去扩展MySQL. 所以UDF是对MySQL功能的一 ...

  8. 【前端】Require.js使用方法总结

    一.为什么要使用require.js 首先一个页面如果在加载多个js文件的时候,浏览器会停止网页渲染,加载文件越多,网页失去响应的时间就会越长:其次,由于js文件之间存在依赖关系,因此必须严格保证加载 ...

  9. LeetCode 217. Contains Duplicate (包含重复项)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  10. 深度学习系列 Part (2)

    1. 神经网络原理 神经网络模型,是上一章节提到的典型的监督学习问题,即我们有一组输入以及对应的目标输出,求最优模型.通过最优模型,当我们有新的输入时,可以得到一个近似真实的预测输出. 我们先看一下如 ...