leetcode 4 - binary search

注意:
1)需要保证nums1 的长度比 nums2 的长度小;(否则vector指针会越界)
2) 当分割线(partition)在首或尾时,用INT_MIN 和 INT_MAX 代替。
思路:

class Solution {
public:
double static findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int x = nums1.size();
int y = nums2.size();
if(x>y)
return findMedianSortedArrays(nums2, nums1);
int l = x + y;
int length = (x + y + ) / ;
double median = ;
//vector x 中:
int start = ;
int end = x;
while (start <= end) {
//cout << start << endl << end << endl;
int p_x = (start + end) / ;
int p_y = length - p_x;
//if p_x is 0 it means nothing is there on left side, use -INF for maxLeftX
//if p_x is length of input then there is nothing on right side, use +INF for minRightX
double maxLeftX = (p_x == ) ? INT_MIN : nums1[p_x - ];
double minRightX = (p_x == x) ? INT_MAX : nums1[p_x];
double maxLeftY = (p_y == ) ? INT_MIN : nums2[p_y - ];
double minRightY = (p_y == y) ? INT_MAX : nums2[p_y];
if (maxLeftX <= minRightY && maxLeftY <= minRightX)
{
if (l % == )
//长度为偶数
{
median = (max(maxLeftX, maxLeftY)+ min(minRightX, minRightY)) / 2.0;
//cout << max(maxLeftX, maxLeftY) << endl << min(minRightX, minRightY) << endl;
}
else
median = max(maxLeftX, maxLeftY);
return median;
}
else if (maxLeftX > minRightY)
end = p_x - ; //nums1的分割线左移
else if (maxLeftY > minRightX)
start = p_x + ; //nums1的分割线右移
}
return -;
}
};
leetcode 4 - binary search的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...
随机推荐
- scala的隐式转换
摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码. 使用方式: 1. ...
- poj 3304 Segments (题意理解出错,错误的只枚举了过线段的直线)
//枚举过每一条线段的直线, //再判断其他线段的点在直线上或被直线穿过 //即求直线与线段相交(叉积) #include<stdio.h> #include<math.h> ...
- python str, list,tuple, dir
Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如: var1 = 'Hello ...
- SQL Server数据库大型应用解决方案总结(转)
出处:http://tech.it168.com/a2012/0110/1300/000001300144.shtml [IT168 技术]随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计 ...
- php eval
// Our PHP code inside a variable $phpCode = ' class Foo { private $name; public function __construc ...
- 在Lua中封装一个调试日志(附lua时间格式)
--自己封装一个Debug调试日志 Debug={} Info={} local function writeMsgToFile(filepath,msg) end function Debug.Lo ...
- GithubPage自定义腾讯404界面
思路来源 之前看到腾讯网络的404和github自带的404,反差很大,于是想自己弄一个了. 过程 github的404一点也不复杂,只是需要在根目录添加一个404.html的文档,或者404.md的 ...
- Verilog MIPS32 CPU(四)-- RAM
Verilog MIPS32 CPU(一)-- PC寄存器 Verilog MIPS32 CPU(二)-- Regfiles Verilog MIPS32 CPU(三)-- ALU Verilog M ...
- IIS 7.5 使用appliaction Initialization
https://blogs.msdn.microsoft.com/amol/2013/01/25/application-initialization-ui-for-iis-7-5/ 待续 正在测试 ...
- python语言的jenkinapi
# coding:utf-8 from jenkinsapi.jenkins import Jenkins # 实例化Jenkins对象,传入地址+账号+密码 j = Jenkins("ht ...