【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, ...
随机推荐
- Session解析
1.除非关闭所有页面 或者超时session才销毁 2.在几个页面之间切换的时候 session保存用户状态. 3.遍历数组时候for循环中从0开始小于长度,不等于长度,用Matlab用习惯了,竟然从 ...
- angularjs的页面拆分思想
//app.js angular.module('MyModule', ['SubModule1', 'SubModule2']) .module('SubModule1', ['CommonModu ...
- codevs1322 单词矩阵
题目描述 Description 对于包含字母A到Y各一次的单词S,将其从上到下从左到右写在一个5*5的矩阵中,如单词ADJPTBEKQUCGLRVFINSWHMOXY写出来如下: A D J P T ...
- [NOIP2010] 提高组 洛谷P1540 机器翻译
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...
- POJ2309BST(树状数组)
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9182 Accepted: 5613 Description C ...
- 领域模型中的实体类分为四种类型:VO、DTO、DO、PO
http://kb.cnblogs.com/page/522348/ 由于不同的项目和开发人员有不同的命名习惯,这里我首先对上述的概念进行一个简单描述,名字只是个标识,我们重点关注其概念: 概念: V ...
- java 打包过程及如何使用第三方jar包
地址:http://wenku.baidu.com/view/44a1bbed81c758f5f61f6779.html或者 http://wenku.it168.com/d_000575231.sh ...
- cocos基础教程(11)事件分发机制
cocos3.0的事件分发机制: 创建一个事件监听器-用来实现各种触发后的逻辑. 事件监听器添加到事件分发器_eventDispatcher,所有事件监听器有这个分发器统一管理. 事件监听器有以下几种 ...
- PHP5 Session 使用详解(一)
http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无 状态,指的是不会维护http请求数据,http请求是独立的,不持久的.而越来越复杂的WEB应用,需要保存一些用户 ...
- 浅谈如何使用Log4j记录日志
一.什么是log4j Log4J是Apache的一个开放源代码的项目.通过使用Log4J,程序员可以控制日志信息输送的目的地,包括控制台,文件,GUI组件和NT事件记录器,也可以控制每一条日志的输出格 ...