Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现
Leetcode平台上的Median of Two Sorted Arrays题目,大意就是找两个已排序数组的中位数。今天先用快排的方式实现一下,代码如下:
There are two sorted arrays A and B 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)).
public class Solution {public static int[] C;public static void quickSort(int a[],int low, int high){if(low < high){int i = low;int tmp;for(int j = low; j< high; j++){if(a[j]<=a[high]){tmp = a[i];a[i] = a[j];a[j] = tmp;++ i;} }tmp = a[i];a[i] = a[high];a[high] = tmp;quickSort(a,low,i-1);quickSort(a,i+1,high);}}public static double findMedianSortedArrays(int A[], int B[]) {double i ;//combine these two arrays into a new array Cint l = A.length+B.length;C = new int[l];System.arraycopy(A, 0, C, 0, A.length);System.arraycopy(B, 0, C, A.length, B.length);quickSort(C,0,l-1);//for(int ii = 0; ii < l; ii ++)//System.out.println(C[ii]);if(l%2 == 0)i = (double) ((C[l/2]+C[l/2-1])/2.0);elsei = (double) C[(l-1)/2];return i;}public static void main(String args[]){int[] A = {1};int[] B = {};double median = findMedianSortedArrays(A,B);System.out.println(median);}
}
快排递归深度logn,每一层的操作复杂度是O(n)所以复杂度应该是O(nlogn)。
对于快排能AC我表示相当意外,只能说leetcode平台给的测试用例不够强壮。如果给一个倒序排好序的数组,我这个快排就死掉了,相当于冒泡阿。
今天再用堆的思想实现一下,复杂度应该比这个快排低一些。
谢谢木易先森的指导!
Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现相关推荐
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...
- 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数
题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...
- LeetCode刷题第二天——3Longest Substring Without repeating character 4 Median of Two Sorted Arrays...
混淆点: 子串 连续 子序列 可以不连续 知识点: HashMap: 出现问题: 1.使用unordered_map头文件时报错 #error This file requires compiler ...
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 算法—两个有序数组的中位数 Median of Two Sorted Arrays
关注微信公众号:CodingTechWork,一起学习进步. 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
- LeetCode上求两个排序数组中位数问题—— Median of Two Sorted Arrays
1.题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- LeetCode Median of Two Sorted Arrays (DFS)
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
最新文章
- 破解win2003“终端服务器授权”激活许可证!
- 【TensorFlow】Anaconda下快速安装TensorFlow教程
- boost::mp11::mp_map_replace相关用法的测试程序
- RequestQueue
- 如何判断Unix系统的一个库文件是32位还是64位的
- 数据库视频(三)——总结篇
- 数字图像处理(拓展)
- SQL Server中 sysobjects、syscolumns、systypes
- LINQ to CSV,一种类型安全,动态的高性能方法
- DataSet运用DES加解密到Xml
- 给老师的作文:育儿经验-父母是孩子最好的老师
- Struts2中过滤器和拦截器的区别
- 链表(python版)
- 宝塔实测-电商ERP进销存系统源码
- 服务器每天自动变密码,Windows自动修改系统密码分享
- Android 密钥库系统 (一)
- linux 临时文件夹设置,Linux 系统 tmp 目录的安全设置
- 【开源】这个人脸生成器厉害了,网红脸、明星脸、萌娃脸通通都有
- 实现App跳转到应用商店
- 美国华盛顿州立大学计算机排名,2020年华盛顿州立大学排名TFE Times美国最佳计算机科学硕士专业排名第38...