[LeetCode][Java] Median of Two Sorted Arrays
来源:互联网 发布:中电数据服务有限公司 编辑:程序博客网 时间:2024/05/22 14:09
题目:
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)).
题意:
合并两个有序数组,并返回新的数组的中位数。
算法分析:
若有n个数,n为奇数,则选择第(n+1)/2个为中位数,若n为偶数,则中位数是(n/2以及n/2+1)的平均数
代码如下:
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int[] list= new int[nums1.length+nums2.length]; double res=0; for(int i=0;i<nums1.length;i++) list[i]=nums1[i]; for(int i=nums1.length;i<nums1.length+nums2.length;i++) list[i]=nums2[i-nums1.length]; Arrays.sort(list); if((nums1.length+nums2.length)%2!=0) res=list[(nums1.length+nums2.length)/2]; else res=(double)(list[(nums1.length+nums2.length)/2]+list[(nums1.length+nums2.length)/2-1])/2; return res; }}
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)
- android发送xml信息到web服务器简单例子
- DB2 DPF 双机搭建
- linux串口编程 非规范模式 read()问题
- Java程序日志二
- 函数式编程
- [LeetCode][Java] Median of Two Sorted Arrays
- NYOJ 96 n-1位数(水题,%*符的学习与使用)
- Getting Started with Activity & Fragment Transitions
- OpenCV2.4.10+VS2013+Win8.1 64位配置
- Xcode的debug调试
- 黑客丛林之旅通关攻略(共14关)
- The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods install
- 常用C++第三方库
- thinkphp url重写