leetcode4:Median of Two Sorted Arrays
来源:互联网 发布:java type类型 编辑:程序博客网 时间:2024/05/29 04:38
本质上就是一个有序数组合并,然后求中位数,不用细讲,直接给代码!
package _20171021;public class Main { public static void main(String[] args) { int[] array1 = {1,26,39,42,52}; int [] array2 = {10,23,45,65,89}; System.out.println(mediaValue(array1, array2)); } public static double mediaValue(int[] a1, int[] a2) { int len1 = a1.length; int len2 = a2.length; int len = len1 + len2; int[] all = new int[len]; int l = 0; //标记a1当前位置 int m = 0; //标记a2当前位置 int n = 0; //标记总的当前位置 while(l < len1 && m < len2) { //有序数组合并 if(a1[l] < a2[m]) { all[n] = a1[l]; n++; l++; } else { all[n] = a2[m]; n++; m++; } } while(l < len1) { all[n] = a1[l]; n++; l++; } while(m < len2) { all[n] = a2[m]; n++; m++; } int mid = 0; if((len & 1) != 0) { //奇数 mid = len / 2; return all[mid] * 1.0; } else { //偶数 mid = len / 2; return (all[mid-1] + all[mid]) * 1.0 / 2; } }}
阅读全文
0 0
- [LeetCode4]Median of Two Sorted Arrays
- leetcode4 Median of two sorted arrays
- leetcode4 Median of Two Sorted Arrays
- LeetCode4:Median of Two Sorted Arrays
- leetcode4:Median of Two Sorted Arrays
- leetcode4~Median of Two Sorted Arrays
- leetcode4. Median of Two Sorted Arrays
- LeetCode4—Median of Two Sorted Arrays
- leetcode4. Median of Two Sorted Arrays
- LeetCode4. Median of Two Sorted Arrays
- leetcode4 Median of Two Sorted Arrays
- Leetcode4. Median of Two Sorted Arrays
- leetcode4:Median of Two Sorted Arrays
- leetcode4. Median of Two Sorted Arrays
- LeetCode4/88 Median of Two Sorted Arrays/Merge Sorted Array
- LeetCode4.Median of Two Sorted Arrays二分查找法
- LeetCode4. Median of Two Sorted Arrays分治法
- Leetcode4: Median of Two Sorted Arrays (求已排序两个数组的中间值)
- 游戏服务器开发的基本体系与服务器端开发的一些建议
- BZOJ4592 线段树
- codeforces
- get请求表单的action属性后不能带参数
- 中缀式 后缀式 前缀式
- leetcode4:Median of Two Sorted Arrays
- 用e ≈1+1/1!+1/2!+1/3!+……求e的近似值,要求误差小于0.00001。
- 数据库访问优化
- 诺基亚7来了, 蔡司认证镜头亮眼,但这样的诺基亚你还会买吗?
- 最强阿尔法狗诞生:自学3天,100:0碾压旧狗
- 【LeetCode1-3】1. Two Sum 2. Add Two Number 3.Longest Substring Without
- es6 异步函数 async/awiat 理解
- Sizeof与Strlen区别
- 搭建后台环境遇到的几个错误及找到的解决办法(需更新)