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 ...
随机推荐
- DWR 3.0 入门示例教程
DWR(Direct Web Remoting) DWR is a Java library that enables Java on the server and JavaScript in a b ...
- C#变量初始化
在C#中声明变量使用下述语法: datatype identifier;, 例如: int i; 该语句声明int变量i.编译器不允许在表达式中使用这个变量,除非用一个值初始化了改变量.如果你不需要使 ...
- Java Persistence with MyBatis 3(中文版) 第一章 MyBatis入门
本章将涵盖以下话题: ž MyBatis是什么? ž 为什么选择MyBatis? ž MyBatis安装配置 ž 域模型样例 1.1 MyBatis是什么 MyBatis是一个简化和实现了Ja ...
- CSS选择器种类及介绍
首先说主都有哪些先择器 1.标签选择器(如:body,div,p,ul,li) 2.类选择器(如:class="head",class="head_logo") ...
- BWA/BWT 比对软件
名称 bwa – Burrows-Wheeler Alignment Tool 内容摘要描述命令行与选项SAM 比对格式短序列比对注意事项 比对精确性 估计插入大小分布 内存需求 ...
- Requests接口测试-对cookies的操作处理(二)
我们继续来讨论一下cookie这方面的内容,我们都知道cookie是数据,一般的话在我接口测试中,数据都是要和代码进行分离的.本篇内容,我们队cookie内容进行处理,我们把登陆成功后的cookie写 ...
- vs2015发布网站至azure web应用服务
进入www.azure.cn管理门户 1,左下角新建web应用,实例如下,url设置为demo(有防止重名判断) 2,进入demo配置页(左侧web应用下点击demo) 3,demo首页,下载配置文件 ...
- 设计模式12: Proxy 代理模式(结构型模式)
Proxy 代理模式(结构型模式) 直接与间接 人们对于复杂的软件系统常常有一种处理手法,即增加一层间接层,从而对系统获得一种更为灵活.满足特定需求的解决方案.如下图,开始时,A需要和B进行3次通信, ...
- MySQL查询表内重复记录并删除
在日常业务场景中,经常会出现一个问题就是解决数据重复的问题,这里用到了一张用户表(s_user)做重复数据操作,分别包含了两个字段,id.name分别用于做唯一标示以及相同姓名的检索. 表结构以及测试 ...
- Linux文件排序工具 sort 命令详解
sort是排序工具,它完美贯彻了Unix哲学:"只做一件事,并做到完美".它的排序功能极强.极完整,只要文件中的数据足够规则,它几乎可以排出所有想要的排序结果,是一个非常优质的工具 ...