leetcode 刷题日记——Median of Two Sorted Arrays
来源:互联网 发布:网络工具书 编辑:程序博客网 时间:2024/06/07 19:39
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看到此题第一反应就是归并排序,然后求出中位数。class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m=nums1.length; int n=nums2.length; int[] com =new int[m+n]; int k=0,i=0,j=0; while( i<m || j<n){ while(i<m&&j<n){ if(nums1[i]<=nums2[j]) com[k++]=nums1[i++]; else com[k++]=nums2[j++]; } while(i<m) com[k++]=nums1[i++]; while(j<n) com[k++]=nums2[j++]; } if((m+n)%2==0) return (double)0.5*(com[(m+n)/2-1]+com[(m+n)/2]); else return (double)com[(m+n)/2]; }}
阅读全文
0 0
- Leetcode刷题日记 -- Median of Two Sorted Arrays
- leetcode 刷题日记——Median of Two Sorted Arrays
- LeetCode — Median of Two Sorted Arrays
- LeetCode—Median of Two Sorted Arrays
- leetcode—Median of Two Sorted Arrays
- Leetcode日记(4)---Median of Two Sorted Arrays
- LeetCode刷题之第二题——Median of Two Sorted Arrays
- 刷题的日常[Leetcode]——4)median of two sorted arrays
- LeetCode刷题笔录 Median Of Two Sorted Arrays
- [leetcode刷题系列]Median of Two Sorted Arrays
- LeetCode —— Median of Two Sorted Arrays
- LeetCode——Median of Two Sorted Arrays
- leetcode 004 —— Median of Two Sorted Arrays
- LeetCode——Median of Two Sorted Arrays
- LeetCode题解——4Median of Two Sorted Arrays
- LeetCode——Median of Two Sorted Arrays
- LeetCode OJ——Median of Two Sorted Arrays
- leetcode——Median of Two Sorted Arrays
- 编程遇到错误的排查方法
- SylixOS启动脚本分析
- 观察颜色变化
- Swift中Timer计时器循环引用问题
- MySQL中基本的多表连接查询教程
- leetcode 刷题日记——Median of Two Sorted Arrays
- 4K图像处理技术之FPGA处理流程(1)
- MFC 自定义类的串行化使能方法
- [Leetcode] 116, 129, 23
- vs2017使用libfdk-aac 链接提示没有“COMDAT 0xF”错误解决
- 自定义Toast
- 【基础算法】 书的复制
- 数位DP专题总结
- PHP 正则表达式常用函数使用小结