4. Median of Two Sorted Arrays
来源:互联网 发布:樱井知香喷泉原因 编辑:程序博客网 时间:2024/06/01 19:56
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]nums2 = [2]The median is 2.0
Example 2:
nums1 = [1, 2]nums2 = [3, 4]The median is (2 + 3)/2 = 2.5
寻找两个有序数组的中位数
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m = nums1.length; int n = nums2.length; int i=0,j=0,k = 0; int a=0,b=0; while (k <= (m+n)/2) { if (i < m && j < n) { a = b; b = nums1[i] < nums2[j] ? nums1[i++] : nums2[j++]; } else if (i < m) { a = b; b = nums1[i++]; } else if (j < n) { a = b; b = nums2[j++]; } k++; } if ((m+n)%2 == 1) { return (double) b; } else { return ((double)a + (double)b)/2; } }}
0 0
- [LeetCode]4.Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays
- LeetCode --- 4. Median of Two Sorted Arrays
- [Leetcode] 4. Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays
- [LeetCode]4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 【leetcode】4. Median of Two Sorted Arrays
- Leetcode-4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- LeetCode-4.Median of Two Sorted Arrays
- leetcoode 4. Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- leetcode 4. Median of Two Sorted Arrays
- 局部加权线性回归
- plugin with id 'com.android.application' not found
- FLV文件分析--1 (头部分析)
- spice debug相关
- ios证书申请
- 4. Median of Two Sorted Arrays
- ubuntu 中 eclipse 的菜单栏 显示问题
- 随机采样--MCMC
- [Codeforces475D]CGCDSSQ(数学相关+二分)
- [蓝桥杯]查找整数+数列特征
- hibernate注解一对多错误
- 腾讯云分布式高可靠消息队列CMQ架构
- Nginx + 阿里云SSL + tomcat 实现https访问代理
- PCIe学习笔记(5)---PCIe的层次结构--DATA LINK LAYER