注意:

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的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  6. [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 ...

  7. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  9. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  10. LeetCode Closest Binary Search Tree Value

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...

随机推荐

  1. 创建一个实例&创建一个线程。。

    using System; using System.Threading; namespace WorkerThread02 { class ThreadTest { bool done; stati ...

  2. python3--多目录之间的协作的一些必备知识

    # Auther: Aaron Fan # 动态获取执行文件的相对路径路径:print(__file__) #动态获取执行文件的绝对路径:import osfile_path = os.path.ab ...

  3. idea hibernate jpa 生成实体类

    0,添加mysql数据库连接 1,生成个hibernate.cfg.xml 2,打开Persisitence 3,Import Databases Schema 4,选择表生成实体类

  4. Unable to locate JAR/zip in file system as specified by the driver definition: ojdbc14.jar

    eclipse的配置错误,把当前包删除,重新导入一个包.然后设置与需要的数据库对应,就可以了

  5. ACM 超级楼梯 发工资

    超级楼梯 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M( ...

  6. gkd

    ## Part 0. 开篇 组长博客:[戳我进入]() 作业博客:[班级博客本次作业的链接](https://edu.cnblogs.com/campus/fzu/Grade2016SE/homewo ...

  7. 【Linux】Vim编辑器

    本文基于Debian 1.vim使用简介 1.1vim安装 使用apt安装vim即可: sudo apt-get install vim 1.2 vim编辑器的模式 vim编辑器分为命令模式和编辑模式 ...

  8. 洛谷 P2596 [ZJOI2006]书架 (splay)

    题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本.由于这些 ...

  9. Arduino I2C + 温湿度传感器Si7021

      Si7021是Silicon Labs生产的温湿度传感器芯片.其主要特性: 湿度精度:误差典型值+/-2%RH,最大值+/-3%RH(0~80%RH),出厂已校正 温度精度:误差典型值+/-0.3 ...

  10. 求帮忙解决封装jquery图片滚动问题

    今天用jquery封装了点击图片滚动,但是发现在屏幕自适应时,图片停在的位置会随着屏幕大小而错位(我引入了pocketgrid.css响应式文件,但没办法去那边修改onsize事件...),求大神.. ...