由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode 中部分binary search 总结
相关主题
Facebook 第一轮电面A Simple Question on Binary Search
为什么我写的binary search 比 linear还慢?leetcode 的 Insert Interval 就是过不了大的
binary search in rotated sorted array有重复时怎么办?Bloomberg 面经
问个电面题求sqrt的binary算法,多谢
有没有人总结过binary search是mid加减1和小于或者等于的情况分类Search in a sorted, rotated list
binary search什么时候用l如何计算sqrt
问一个关于binary search的问题我对G有心理阴影。。
问一个search in rotated array的问题大牛看看为撒这个sqrt binary search过不了OJ
相关话题的讨论汇总
话题: search话题: int话题: start话题: mid话题: target
进入JobHunting版参与讨论
1 (共1页)
x*****0
发帖数: 452
1
基本的binary search有三种变形。
(1) 当无法找到target时,返回insert position。对应leetcode中的 search insert
position
(2) 找到第一次出现target。对应于leetcode中 search for a range和sqrt(x)
(3) 找到最后一次出现的target。对应于leetcode中search for a range
今天下午写了一篇文章,探讨用一种模式写出这三种变形。
http://fihopzz.blogspot.com/2013/07/enter-post-title-here-binar
大家走过路过,不要错过。能提出修改意见,那就更好了。
x*****0
发帖数: 452
2
ding!
s*********d
发帖数: 2406
3
ding
g***j
发帖数: 1275
4
我的基本framework是这样的
int xxxx(int x) {
int start = xxx; // the start point to search, for sqrt, it's 1
// generally it's 0, means the first element;

int end = xxx; // the end point to search, for sqrt, it's x
// generally it's n-1, means the last element;

int mid = 0;

while(start <= end ) { // = is needed here

mid = start + (end - start)/2;

if( target == A[mid]) {
return mid; // other stuff for search a range
} else if ( target > A[mid] ) {
start = mid+1;
} else
end = mid-1;

}
return XXXX; // for sqrt, return end, because here end < start
// for insert position, return start
// otherwise, return -1, means not found
}

insert

【在 x*****0 的大作中提到】
: 基本的binary search有三种变形。
: (1) 当无法找到target时,返回insert position。对应leetcode中的 search insert
: position
: (2) 找到第一次出现target。对应于leetcode中 search for a range和sqrt(x)
: (3) 找到最后一次出现的target。对应于leetcode中search for a range
: 今天下午写了一篇文章,探讨用一种模式写出这三种变形。
: http://fihopzz.blogspot.com/2013/07/enter-post-title-here-binar
: 大家走过路过,不要错过。能提出修改意见,那就更好了。

M*******u
发帖数: 51
5
总结的很好,Mark
f*******b
发帖数: 520
6
re
r********d
发帖数: 7742
7
很上心,学习了,赞!
我刚才正好在写search for range,这是我写的search for start和你的不太一样。
int BinarySearchStart(int A[], int lo, int hi, const int &target) {
if (lo > hi) return -1;
while (hi >= lo) {
int mi = lo + (hi-lo)/2;
if (target == A[mi] && (mi-1 < lo || A[mi-1] < target)) {
return mi;
} else if (A[mi] < target) { //have to do this one first
lo = mi+1;
} else {
hi = mi-1;
}
}
return -1;
}
还有就是search rotated,median, 和那个找K in 2 sorted的也都是bsearch。。。或
许也可以总结一下。

insert

【在 x*****0 的大作中提到】
: 基本的binary search有三种变形。
: (1) 当无法找到target时,返回insert position。对应leetcode中的 search insert
: position
: (2) 找到第一次出现target。对应于leetcode中 search for a range和sqrt(x)
: (3) 找到最后一次出现的target。对应于leetcode中search for a range
: 今天下午写了一篇文章,探讨用一种模式写出这三种变形。
: http://fihopzz.blogspot.com/2013/07/enter-post-title-here-binar
: 大家走过路过,不要错过。能提出修改意见,那就更好了。

r******e
发帖数: 132
8
赞啊。学习了。

insert

【在 x*****0 的大作中提到】
: 基本的binary search有三种变形。
: (1) 当无法找到target时,返回insert position。对应leetcode中的 search insert
: position
: (2) 找到第一次出现target。对应于leetcode中 search for a range和sqrt(x)
: (3) 找到最后一次出现的target。对应于leetcode中search for a range
: 今天下午写了一篇文章,探讨用一种模式写出这三种变形。
: http://fihopzz.blogspot.com/2013/07/enter-post-title-here-binar
: 大家走过路过,不要错过。能提出修改意见,那就更好了。

1 (共1页)
进入JobHunting版参与讨论
相关主题
大牛看看为撒这个sqrt binary search过不了OJ有没有人总结过binary search是mid加减1和小于或者等于的情况分类
leetcode里面的rotate array的[1,2], 3是什么意思?binary search什么时候用l
问个经典问题的improvement问一个关于binary search的问题
小公司软工第一轮电面问一个search in rotated array的问题
Facebook 第一轮电面A Simple Question on Binary Search
为什么我写的binary search 比 linear还慢?leetcode 的 Insert Interval 就是过不了大的
binary search in rotated sorted array有重复时怎么办?Bloomberg 面经
问个电面题求sqrt的binary算法,多谢
相关话题的讨论汇总
话题: search话题: int话题: start话题: mid话题: target