Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目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的更多相关文章
- 算法题之Median of Two Sorted Arrays
这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低... 题目: There are two sorted arrays nums1 ...
- 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 ...
- 刷题4. Median of Two Sorted Arrays
一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题 ...
- Kotlin实现LeetCode算法题之Two Sum
LeetCode介绍 LeetCode是算法练习.交流等多功能网站,感兴趣的同学可以关注下(老司机请超车).页面顶部的Problems菜单对应算法题库,附带历史通过滤.难易程度等信息. 未来计划 打算 ...
- 【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 ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- J2EE走向成功路-01-Struts2 配置
配置环境 1. 导入jar包,Struts2官网:http://struts.apache.org/ 2. 创建Action类 继承ActionSupport (ActionSupport类是 ...
- 在 Tomcat 8 部署多端口项目
一般的部署途径 Tomcat 的部署途径很多,一般有如下几种: 直接将 War 包拷贝到 webapps 目录中,然后启动 Tomcat. 登陆 Tomcat 管理控制台http://localhos ...
- jenkins、ant、selenium、testng搭建自动化测试框架
如果在你的理解中自动化测试就是在eclipse里面讲webdriver的包引入,然后写一些测试脚本,这就是你所说的自动化测试,其实这个还不能算是真正的自动化测试,你见过每次需要运行的时候还需要打开ec ...
- pm2部署多个nodejs项目配置教程
实际项目部署中,我们服务器在启动的时候需要自动启动node服务.以前是通过liunx自带的命令启动.但是随着后台微服务越来越多.每次发布新程序.修改脚本太麻烦了.于是换成PM2来做. 1.首先安装pm ...
- 我的three.js学习记录(三)
此次的亮点不是three.js的3d部分,而是通过调用摄像头然后通过摄像头的图像变化进行简单的判断后进行一些操作.上篇中我通过简单的示例分析来学习three.js,这次是通过上一篇的一些代码来与摄像头 ...
- 在 Ubuntu 14.04 中安装 Pepper Flash Player For Chromium
转自:http://blog.csdn.net/ygzhong000/article/details/26160953
- java zip解压
/** * 解压文件到指定目录 * @param zipFile * @param descDir * @author sqdll */@SuppressWarnings("rawtypes ...
- 242. Valid Anagram(leetcode)
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- spark的sparkUI如何解读?
spark的sparkUI如何解读? 以spark2.1.4来做例子 Job - schedule mode 进入之后默认是进入spark job 页面 这个说明有很详细的解释,spark有两种操作算 ...