LeetCode 4 双数组找中位数
来源:互联网 发布:咖啡网络课程 编辑:程序博客网 时间:2024/06/06 16:26
虽然是hard 但是我觉得比前面都简单
运用了归并排序 其实就是分治算法书上的例题最后一步 计算复杂度是O(n)然后找中位数就好
c# code
public class Solution
{
public double FindMedianSortedArrays(int[] nums1, int[] nums2)
{
if(nums1.Length==0&&nums2.Length==0)
{
return 0;
}
int[] array=new int[nums1.Length+nums2.Length];
int index=0,a=0,b=0;
while(a<nums1.Length&&b<nums2.Length)
{
if(nums1[a]<=nums2[b])
{
array[index]=nums1[a];
a++;
index++;
}
else
{
array[index]=nums2[b];
b++;
index++;
}
}
if(a<nums1.Length&&nums1.Length==nums2.Length)
{
array[index]=nums1[a];
}
if(b<nums2.Length&&nums1.Length==nums2.Length)
{
array[index]=nums2[b];
}
if(nums1.Length>nums2.Length)
{
while(a<nums1.Length)
{
array[index]=nums1[a];
a++;
index++;
}
}
if(nums1.Length<nums2.Length)
{
while(b<nums2.Length)
{
array[index]=nums2[b];
b++;
index++;
}
}
double result=0;
if(array.Length%2==0)
{
result=((double)array[(array.Length-1)/2]+(double)array[(array.Length)/2])/2.0;
}
else
{
result=(double)array[(array.Length-1)/2];
}
return result;
}
}
- LeetCode 4 双数组找中位数
- 双数组找中位数
- openjudge 双数组找中位数
- leetcode 4两数组找中位数
- 找中位数
- 找中位数
- 海量数据找中位数
- Sicily 4224. 找中位数
- 海量数据找中位数
- 大数据量找中位数
- 大根堆 小根堆 找中位数
- 海量数据找中位数
- 无序数组找中位数
- 海量数据找中位数
- 面试常见问题-找中位数
- 二分法找中位数
- 关于双数组Trie
- 双数组trie数据结构
- eclipse使用相关
- spring mvc 406错误
- Scala教程(一)走进Scala世界
- Arcgis for javascript实现百度地图ABCD marker的效果
- [android developer 学习笔记]monkey test
- LeetCode 4 双数组找中位数
- 使用adb connect对appium进行无线自动化测试
- wordpress相关路径
- linux C++ 使用zlib 压缩字符串
- java笔记
- SQLServer中DateTime转换成Varchar样式
- Yii2 配置框架国际化支持
- android java 判断是否同一天
- 字符串匹配的KMP算法、Boyer-Moore算法