【Shawn-LeetCode】4.Median of Two Sorted Arrays
来源:互联网 发布:java webservice 调试 编辑:程序博客网 时间:2024/06/04 20:04
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
Subscribe to see which companies asked this question.
题目是很简单,但是如果把复杂度控制在log(m+n)的范围内,肯定是有问题的,先贴一个超时的算法
public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int[] nums3=new int[nums1.length+nums2.length];System.arraycopy(nums1,0, nums3, 0, nums1.length);System.arraycopy(nums2,0, nums3, nums1.length, nums2.length);System.out.println(Arrays.toString(nums3));Arrays.sort(nums3);if(nums3.length%2==0){return (nums3[nums3.length/2-1]+nums3[nums3.length/2])/2.0;}else return nums3[nums3.length/2]; }}
正在探索新的方法
阅读全文
0 0
- 【Shawn-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
- 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
- web service技术之 soap
- 正则表达式大全
- 关于android相关开发资料的整理
- Several ports (8005, 8080, 8009) required by Tomcat v8.0 Server at localhost are already in use.
- yii json_encode
- 【Shawn-LeetCode】4.Median of Two Sorted Arrays
- 每日读报
- cookie的domain实战
- Java笔试题解(13)
- 三分钟了解工作流Activiti
- rtsp协议
- Android--仿淘宝商品详情(继续拖动查看详情)及标题栏渐变
- 访问页面时queueing为5秒的问题
- 单例模式的理解