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 ...
随机推荐
- [SoapUI] 比较两个不同环境下的XML Response, 从外部文件读取允许的偏差值,输出结果到Excel
import static java.lang.Math.* import java.text.NumberFormat import java.awt.Color import com.eviwar ...
- JS作用域理解(声明提升)
1.JS解析步骤: a.预解析 将变量声明提升: 将函数声明及函数内容提升,可以理解成原来位置的函数在解析代码时已经提到代码初始位置: 块内的变量声明和函数声明也会被提升,例如if语句 遇到重名,只留 ...
- OSG相机与视图
转自:http://blog.csdn.net/wang15061955806/article/details/51603083 相机与视图 osg::Camera类用来管理OSG中的模型—— ...
- javascript总结46: JS三大系列-方便的offset 家族
1 offset 家族 offset这个单词本身是--偏移,补偿,位移的意思. js中有一套方便的获取元素尺寸的办法就是offset家族: 2 offset 结构介绍为: 3 offset常用属性 ...
- 搜狐 WEB 标准-前端技术应用规范
- SQl语句收藏
/* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...
- C#读取Excel的数据,并且以混合模式读取,防止数据类型变更
/// <summary> /// Read Excel to DataSet /// </summary> /// <param name="filename ...
- MVC断点续传
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...
- WebService 常用的设置
1.修改WebService接收长度 <binding name="IAuthServiceSoap11Binding" maxBufferSize="214748 ...
- 用Visual Studio 2015成功编译、发布UMDF驱动到目标机!!
开发工具:Visual Studio 2015企业版 主 机:windows10 X64企业版,主机是安装了Visual Studio 2015的操作系统,主要进行驱动开发和调试. 目 标 ...