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 ...
随机推荐
- 深入理解yield-乾颐堂
yield的英文单词意思是生产,刚接触Python的时候感到非常困惑,一直没弄明白yield的用法. 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: 1 2 3 def ad ...
- MicroRNA 详解
MicroRNA研究历史和方法 Views 88 1Report
- C#事件订阅及触发例子
最典型的事件例子,猫叫了(事件源),老鼠跑了(事件订阅者),惊醒主人(事件订阅者) 源代码: class Program { static void Main(string[] args) { Cat ...
- 'for' loop initial declarations are only allo
linux系统下的c编程与windows有所不同,如果你在用gcc编译代码的时候提示‘for’ loop initial declarations are only allowed in C99 mo ...
- SQl语句收藏
/* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...
- Ajax轮询消息自动提示(消息盒子)
经过一下午写了个消息盒子的例子,用的是ajax方式轮询读取,没有用到后台自动“推”数据的方式,效果良好. <%@ Page Language="C#" AutoEventWi ...
- phpize命令在安装AMQP插件是报错phpize:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF envir的解决方法
phpize命令在安装AMQP插件是报错phpize:Cannot find autoconf. Please check your autoconf installation and the $PH ...
- linux 管道与重定向
命令行shell数据流有如下定义: 通过管道和重定向可以控制CLI的数据流
- LoadRunner10个用户并发测试时分别取不同的参数运行脚本
使用场景,比如说10个用户使用不同的账户名和密码同时并发登录,此时选择如下参数化策略: 参数选择:select next row 选择unique update value on 选择 once 另一 ...
- [js]利用闭包向post回调函数传参数
最近在闲逛校园XX站的时候,打算搞个破坏,试试有多少人还是用初始密码登陆.比较懒,所以直接打开控制台来写. 所以问题可以描述为: 向后端不断的post数据,id从1~5000自增,后端会根据情况来返回 ...