【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays
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)).
解题思路:
由于要求时间复杂度O(log (m+n))所以几乎可以肯定是递归和分治的思想。
《算法导论》里有找两个数组第K小数的算法,时间复杂度为O(log(m+n)),所以直接调用即可
参考链接:http://blog.csdn.net/yutianzuijin/article/details/11499917/
Java参考代码:
public class Solution {
public static double findKth(int[] nums1, int index1, int[] nums2,
int index2, int k) {
if (nums1.length - index1 > nums2.length - index2)
return findKth(nums2, index2, nums1, index1, k);
if (nums1.length - index1 == 0)
return nums2[index2 + k - 1];
if (k == 1)
return Math.min(nums1[index1], nums2[index2]);
int p1 = Math.min(k / 2, nums1.length - index1), p2 = k - p1;
if (nums1[index1 + p1 - 1] < nums2[index2 + p2 - 1])
return findKth(nums1, index1 + p1, nums2, index2, k - p1);
else if (nums1[index1 + p1 - 1] > nums2[index2 + p2 - 1])
return findKth(nums1, index1, nums2, index2 + p2, k - p2);
else
return nums1[index1 + p1 - 1];
} static public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if ((nums1.length + nums2.length) % 2 != 0)
return findKth(nums1, 0, nums2, 0,
(nums1.length + nums2.length) / 2 + 1);
else
return findKth(nums1, 0, nums2, 0,
(nums1.length + nums2.length) / 2)
/ 2.0
+ findKth(nums1, 0, nums2, 0,
(nums1.length + nums2.length) / 2 + 1) / 2.0;
}
}
C++实现如下:
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
private:
double findKth(vector<int> nums1, int index1, vector<int> nums2, int index2, int k) {
if (nums1.size() - index1 > nums2.size() - index2) {
swap(nums1, nums2);
swap(index1,index2);
}
if (nums1.size() - index1 == )
return nums2[index2 + k - ];
if (k == )
return min(nums1[index1], nums2[index2]);
int p1 = min(k / , (int)nums1.size() - index1), p2 = k - p1;
if (nums1[index1 + p1 - ] < nums2[index2 + p2 - ])
return findKth(nums1, index1 + p1, nums2, index2, k - p1);
else if (nums1[index1 + p1 - ] > nums2[index2 + p2 - ])
return findKth(nums1, index1, nums2, index2 + p2, k - p2);
else
return nums1[index1 + p1 - ];
} public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if ((nums1.size() + nums2.size()) &)
return findKth(nums1, , nums2, ,
(nums1.size() + nums2.size()) / + );
else
return findKth(nums1, , nums2, ,(nums1.size() + nums2.size()) / )/ 2.0
+ findKth(nums1, , nums2, ,(nums1.size() + nums2.size()) / + ) / 2.0;
}
};
【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays的更多相关文章
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- 你也可以当面霸-Servlet与JSP的原理及特点
既然是面试系列,就是面试官和应聘者之间的对话.本文是采用一问一答的形式呈现给读者的,这样能有一个明确的考察点,不像理论知识那么枯燥. 01.什么是Servlet技术 Servlet是和平台无关的服务器 ...
- BZOJ-3229 石子合并 GarsiaWachs算法
经典DP?稳T 3229: [Sdoi2008]石子合并 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 426 Solved: 202 [Submit] ...
- Codeforces 650C Table Compression
传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ...
- 在.NET使用JSON作为数据交换格式
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://zhoufoxcn.blog.51cto.com/792419/517093 我们 ...
- jQueryEasyUI DateBox的基本使用
http://www.cnblogs.com/libingql/archive/2011/09/25/2189977.html 1.基本用法 代码: 1 2 3 4 5 6 7 8 9 10 11 1 ...
- mongo(删除操作)
在使用MongoDB的时候,经常会用到MongoDB的删除操作,以下是我在使用MongoDB删除操作的总结 首先是删除用户: db.removeUser("用户名") 其次是删除数 ...
- js校验表单后提交表单的三种方法总结
第一种: 复制代码 代码如下: <script type="text/javascript"> function check(form) { if(fo ...
- JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)
截取字符串: var str = "1234567890"; var a = str.substring(0,8); //==str.substring(8)---结果:12 ...
- Android文件保存和读取
public class DataActivity extends Activity { private EditText filenameText; private EditText content ...
- MyBatis 用户表记录数查询
搭建MyBatis开发环境,实现用户表记录数查询 1.在MyEclipse中创建工程,导入MyBatis的jar包