leetcode4:Median of Two Sorted Arrays
本质上就是一个有序数组合并,然后求中位数,不用细讲,直接给代码!
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;}}
}
leetcode4:Median of Two Sorted Arrays相关推荐
- LeetCode4. Median of Two Sorted Arrays(二分法)
题解 划分元素组 长数组a,短数组b 数组 元素组1 元素组2 a(长度为m) a1,a2,a3-ai ai+1,ai+2-am-1,am b(长度为n) b1,b2,b3-bj bj+1,bj+2- ...
- 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数
题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...
- LeetCode刷题第二天——3Longest Substring Without repeating character 4 Median of Two Sorted Arrays...
混淆点: 子串 连续 子序列 可以不连续 知识点: HashMap: 出现问题: 1.使用unordered_map头文件时报错 #error This file requires compiler ...
- 算法—两个有序数组的中位数 Median of Two Sorted Arrays
关注微信公众号:CodingTechWork,一起学习进步. 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现
Leetcode平台上的Median of Two Sorted Arrays题目,大意就是找两个已排序数组的中位数.今天先用快排的方式实现一下,代码如下: There are two sorted ...
- LeetCode Median of Two Sorted Arrays (DFS)
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- Median of Two Sorted Arrays
problem: There are two sorted arrays nums1 and nums2 of size m and n respectively. 有两个排序好的数列num1和num ...
最新文章
- MAPREDUCE实践篇(2)
- js实现反恐精英+曲线图+饼状图
- 【Android 逆向】类加载器 ClassLoader ( Android 的八种类加载器 | ClassLoader | BaseDexClassLoader | DexClassLoader )
- c语言链表编程作业,C语言编程入门——链表
- android v4包自动导入吧,android如何导入v4包的源码
- 如何将SQL Profiler Trace读入到SQL的表中?
- poj3616(Milking Time)
- WIN7不能访问共享文件夹
- icloud 照片同步_如何确保Mac和iPhone的照片同步到iCloud
- windows 生成ssh 密钥
- 论文阅读:在Stiefel流形上的黎曼优化
- 常用的Web前端技术有哪些?如何入门?
- PCIE及南桥芯片组
- 宇宙最全面试题目实录(二)
- Linux中的defunct进程(僵尸进程)
- 《教学设计》考试内容
- java 分布式介绍
- 2019年山东事业单位面试真题
- 软件开发人员不愿意写文档
- 教你正确选择光伏组件与逆变器!