日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

發(fā)布時間:2024/4/14 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Kotlin实现LeetCode算法题之Median of Two Sorted Arrays 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

題目Median of Two Sorted Arrays(難度Hard

?

?

方案1,數(shù)組合并&排序調(diào)用Java方法

1 import java.util.* 2 3 class Solution { 4 fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double { 5 val lenNums1 = nums1.size 6 val lenNums2 = nums2.size 7 8 val array = Arrays.copyOf(nums1, lenNums1 + lenNums2) 9 System.arraycopy(nums2, 0, array, lenNums1, lenNums2) 10 11 Arrays.sort(array) 12 13 var median: Double 14 val lenArray = array.size 15 if (lenArray % 2 == 1) { 16 median = array[(lenArray - 1) / 2].toDouble() 17 } else { 18 median = (array[(lenArray - 1) / 2] + array[lenArray / 2]).toDouble() / 2 19 } 20 21 return median 22 } 23 }

?

提交詳情1

?

平均耗時0.25ms。

?

方案2,數(shù)組合并&排序調(diào)用Kotlin方法

1 class Solution { 2 fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double { 3 val lenNums1 = nums1.size 4 val lenNums2 = nums2.size 5 6 val array = IntArray(lenNums1 + lenNums2) 7 System.arraycopy(nums1, 0, array, 0, lenNums1) 8 System.arraycopy(nums2, 0, array, lenNums1, lenNums2) 9 10 array.sort() 11 12 var median: Double 13 val lenArray = array.size 14 if (lenArray % 2 == 1) { 15 median = array[(lenArray - 1) / 2].toDouble() 16 } else { 17 median = (array[(lenArray - 1) / 2] + array[lenArray / 2]).toDouble() / 2 18 } 19 20 return median 21 } 22 }

?

提交詳情2

平均耗時0.27ms。

?

Java & Kotlin代碼對比

其實,通過源碼可以發(fā)現(xiàn),方案1和2在對數(shù)組進行合并與排序時調(diào)用的方法是一樣的。

Arrays.java

1 public static int[] copyOf(int[] original, int newLength) { 2 int[] copy = new int[newLength]; 3 System.arraycopy(original, 0, copy, 0, 4 Math.min(original.length, newLength)); 5 return copy; 6 }

?copyOf方法內(nèi)部調(diào)用的還是System的靜態(tài)方法arraycopy(native就不往下追了)。

?

System.java

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

?

Arrays.kt

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

IntArray(size: Int)會生成一個大小為size,元素值由init方法利用下標值計算而來,如果init不傳入,那么默認均為0。

?

Arrays.kt

1 public fun IntArray.sort(): Unit { 2 if (size > 1) java.util.Arrays.sort(this) 3 }

Kotlin中IntArray的擴展方法sort,內(nèi)部調(diào)用的是Java中Arrays的sort方法。

?

Arrays.java

1 public static void sort(int[] a) { 2 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 3 }

Arrays的sort方法最終是通過快排來實現(xiàn)的。而快速排序的時間復(fù)雜度為O(nlog(n)),但是題目要求量級為O(log(m+n))。

?

方案3,分治法求中位數(shù)

1 class Solution { 2 fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double { 3 var media1: Int 4 var media2 = 0 5 val len1 = nums1.size 6 val len2 = nums2.size 7 8 if ((len1 + len2) % 2 == 1) { 9 media1 = getMedian(nums1, nums2, 0, len1 - 1, 0, len2 - 1, (len1 + len2) / 2 + 1) 10 return media1 / 1.0 11 } else { 12 media1 = getMedian(nums1, nums2, 0, len1 - 1, 0, len2 - 1, (len1 + len2) / 2) 13 media2 = getMedian(nums1, nums2, 0, len1 - 1, 0, len2 - 1, (len1 + len2) / 2 + 1) 14 return (media1 + media2) / 2.0 15 } 16 } 17 18 fun getMedian(nums1: IntArray, nums2: IntArray, s1: Int, n1: Int, s2: Int, n2: Int, k: Int): Int { 19 val x = (s1 + n1) / 2 20 val y = (s2 + n2) / 2 21 22 23 if (s1 > n1) 24 return nums2[s2 + k - 1] 25 26 if (s2 > n2) 27 return nums1[s1 + k - 1] 28 29 return if (nums1[x] <= nums2[y]) { 30 if (k <= x - s1 + (y - s2) + 1) { 31 getMedian(nums1, nums2, s1, n1, s2, y - 1, k) 32 } else { 33 getMedian(nums1, nums2, x + 1, n1, s2, n2, k - (x - s1) - 1) 34 } 35 } else { 36 if (k <= x - s1 + (y - s2) + 1) { 37 getMedian(nums1, nums2, s1, x - 1, s2, n2, k) 38 } else { 39 getMedian(nums1, nums2, s1, n1, y + 1, n2, k - (y - s2) - 1) 40 } 41 } 42 } 43 }

?

提交詳情3

平均耗時0.32ms。

?

結(jié)果分析

但從LeetCode的測試用例所消耗的時間來看,上述三種方案沒有明顯的區(qū)別,理論上分治法的時間復(fù)雜度為O(log(n))。

轉(zhuǎn)載于:https://www.cnblogs.com/tgyf/p/7810376.html

總結(jié)

以上是生活随笔為你收集整理的Kotlin实现LeetCode算法题之Median of Two Sorted Arrays的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。