leetcode Median of Two Sorted Arrays(Java)
来源:互联网 发布:洗衣机推荐 知乎 编辑:程序博客网 时间:2024/05/23 02:25
题目链接:点击打开链接
类型:合并数组
解法:遍历数组,排列生成新的数组
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int len1 = nums1.length; int len2 = nums2.length; int len3 = len1 + len2; int[] nums3 = new int[len3]; for (int i=0, j=0, k=0 ; k<len3 ;) { if (i < len1 && j < len2) { nums3[k] = (nums1[i] < nums2[j]) ? nums1[i++] : nums2[j++]; ++k; } if (i == len1 && j < len2 && i < len3) { nums3[k] = nums2[j++]; ++k; } if (i < len1 && j == len2 && j < len3) { nums3[k] = nums1[i++]; ++k; } } double result = (len3%2==0)?((nums3[len3/2]+nums3[len3/2 - 1])*1.0/2):nums3[len3/2]; return result; }}
此外,在搜索题解时,找到了一种通过寻找第k小数进行求解的方法,现记录如下:
public class Solution { public double findKelemnt(int[] a, int astart, int aend, int[] b, int bstart, int bend, int k){int m = aend - astart + 1;int n = bend - bstart + 1;if (m > n)return findKelemnt(b, bstart, bend, a, astart, aend, k);if (m == 0)return b[k - 1];if (k == 1)return Math.min(a[astart], b[bstart]);int partA = Math.min(k / 2, m);int partB = k - partA;if (a[astart + partA - 1] < b[bstart + partB - 1]){return findKelemnt(a, astart + partA, aend, b, bstart, bend, k - partA);} else if (a[astart + partA - 1] > b[bstart + partB - 1]){return findKelemnt(a, astart, aend, b, bstart + partB, bend, k - partB);} else{return a[astart + partA - 1];}} public double findMedianSortedArrays(int[] nums1, int[] nums2) {double retDouble = 0.0;int len1 = nums1.length;int len2 = nums2.length;int len3 = len1 + len2;int k = len3 / 2;if (len3 % 2 == 0){retDouble = (findKelemnt(nums1, 0, len1 - 1, nums2, 0, len2 - 1, k + 1)+ findKelemnt(nums1, 0, len1 - 1, nums2, 0, len2 - 1, k)) / 2;} else{retDouble = findKelemnt(nums1, 0, len1 - 1, nums2, 0, len2 - 1, k + 1);}return retDouble; }}
阅读全文
0 0
- [Leetcode] Median of Two Sorted Arrays (Java)
- [LeetCode][Java] Median of Two Sorted Arrays
- Median of Two Sorted Arrays --leetcode java
- LeetCode : Median of Two Sorted Arrays [java]
- leetcode:Median of Two Sorted Arrays 【Java】
- [LeetCode]Median of Two Sorted Arrays(Java)
- leetcode Median of Two Sorted Arrays(Java)
- Leetcode-Median of Two Sorted Arrays (java)
- LeetCode – Median of Two Sorted Arrays Java
- [leetcode NO.2] Median of Two Sorted Arrays (JAVA)
- leetcode第四题:Median of Two Sorted Arrays (java)
- 【leetcode with java】4 Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays Java
- leetcode 004 Median of Two Sorted Arrays(java)
- LeetCode(java)4. Median of Two Sorted Arrays
- [LeetCode][4]Median of Two Sorted Arrays解析 -Java实现
- leetcode: 4. Median of Two Sorted Arrays (java)
- Leetcode 第四题< Median of Two Sorted Arrays>(Java)
- 使用Tesseract-OCR训练文字识别记录
- AssetBundle 坑来坑去(一)
- 安信可A7模块、DB9公母头及TTL\rs232简介
- java_Comparator接口
- 布局优化必备 Hierarchy Viewer 工具使用
- leetcode Median of Two Sorted Arrays(Java)
- G-CNN详解
- NSA承包商雇员遭FBI逮捕,俄罗斯干预美国大选内部机密文件曝光
- Listview多tab上滑悬浮
- 微信小程序访问豆瓣电影api 400 (Bad Request)错误解决方法
- Unity3d Action的最最最简单的使用
- 美容美白化妆产品类网站织梦模板【带手机端】
- 用mongodb构建延时队列
- 使用Git上传代码到GitHub