Leetcode-4:Median of Two Sorted Arrays
来源:互联网 发布:js 反混淆 在线 编辑:程序博客网 时间:2024/06/06 01:24
Question:
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)).
有两个已经排序好的数组nums1和nums2,大小分别为m和n。查找两个排序数组的中位数。程序运行时复杂度应该是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
Answer:
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int length = nums1.length + nums2.length; int array[] = new int[length]; int i,j; for(i=0;i<nums1.length;i++){ array[i] = nums1[i]; } for(j=0;j<nums2.length;i++,j++){ array[i] = nums2[j]; } Arrays.sort(array);// for(int x=0; x<length; x++){// System.out.print(array[x]+" ");// } if(length%2 != 0){ return (double)array[(length+1)/2-1]; } else { double ans = (double)((array[length/2]+array[(length/2)-1])/2.0); return ans; } }}
阅读全文
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
- [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
- [Leetcode]4Median 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
- 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
- LeetCode 4 - Median of Two Sorted Arrays
- leetcode 4 -- Median of Two Sorted Arrays
- CSDN-markdown编辑器:MarkDown语法
- Linux 关于邮件发送(使用外部邮箱)
- 有道词典7.2电脑版去广告
- 冒泡排序
- 一个简单的makefile示例及其注释(转载)
- Leetcode-4:Median of Two Sorted Arrays
- MAT打开过大的dump:Java heap space OutOfMemory
- 算法-排序之希尔排序
- 开源modem应用协议栈ofono
- js深度拷贝
- 页面对象模式 Page Object
- Netty 简例
- 九个Console命令,让 JS 调试更简单
- TensorFlow学习--tf.session.run()