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 ...
随机推荐
- 遍历properties文件
Properties pro = new Properties();try { InputStream inStr = ClassLoader.getSystemResourceAsStream ...
- cookie与session组件
会话跟跟踪技术 cookie介绍 Djanjo中操作Cookle Session Django中Session相关方法 Django中的Session配置 CBV中加装饰器 session中运用aja ...
- Git & TortoiseGit
http://www.git-scm.com/download/ http://download.tortoisegit.org/ https://help.github.com/articles/g ...
- C# 过滤SQL 字符串中的 参数
/// <summary> /// 参数过滤 /// </summary> /// <param name="parameters"></ ...
- 【重要】拷贝NIOS II工程后,修改工程前必须要做的10件事
1.拷贝现有工程到新的目录2.打开Quartus II工程文件3.打开NIOS II EDS软件4.切换工作空间到当前的新工程根目录5.在NIOS II EDS软件中将已有的软件工程先删除掉(不要勾选 ...
- [leetcode] 4. Path Sum
终于到了二叉树.题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ...
- 生成文件的MD5、SHA、SHA256
生成文件的MD5.SHA.SHA256 Linux系统生成MD5.SHA.SHA256md5sum file1.zip >> MD5.txt sha1sum file1.zip > ...
- OI树上问题 简单学习笔记
判断链 每个点的度数不超过2 判断树 n个点,n-1条边 每两个点之间的路径唯一 多叉树转换成二叉树 第一个孩子作为左孩子,第一个孩子的兄弟作为它的右孩子. 树的重心 树上一点,满足删除该点时,树内剩 ...
- Public Bike Management (30)(DFS,VRCTOR,模拟)(PAT甲级)
#include<bits/stdc++.h>using namespace std;const int inf = 1e9;int sum,n,tar,m;int num[507];in ...
- 【OCP|052】052考试题库又变了,最新052题库收集整理-第15题
15.Which two are true about space management in tablespaces? A) Locally managed tablespaces have eit ...