由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一个问题的答案,好像以前有人讨论过
相关主题
Google电话面试题目一个小公司面经
请教个面试题问个面试题
[合集] Google电话面试题目问一道老题
[算法] unsorted arraya[i] + b[j] = c[k] 的题有靠谱的答案不?
triplets 题怎么做比较高效?请教一道题目
Another amazon interview questions请教一道题
array contains two integer that sum up to 7请教一个常见的面试题的答案
算法题:两列找共同元素有O(n)的算法吗?O(N) sort integer array
相关话题的讨论汇总
话题: array话题: found话题: ub话题: lb话题: index
进入JobHunting版参与讨论
1 (共1页)
K******g
发帖数: 1870
1
You have an array A of unknown length L. The array contains numbers that
are distinct and sorted ascendingly. The array is 1-indexed i.e. first
element is got by A[1] and not A[0]. To help u, since L is not known, when
an index out of bounds is used, then a special value of NOT_FOUND is
returned. The question is to find some positive integer n such that when
used as the index, the result from the array is the n itself. i.e. A[n] = n.
Z*****Z
发帖数: 723
2
【我是这样想的,对任意的i
如果A[i] == i,那么i就是要找的
如果A[i] > i或者A[i]==NOT_FOUND, 那么这样的数只能是在i之前
如果A[i] < i,那么这个解应该在i之后(如果存在的话)
找一个k,使得这个解在2^k和2^(k+1)之间,然后二分查找
在 KingMing (洱东金茗) 的大作中提到: 】
n.
y*c
发帖数: 904
3
i=1;
while(A[i]!=Not_found&&A[i]<=i){
if(A[i]==i)
return i;
i *= 2;
}
if(A[i]>i)
return -1;
// then binary search in [i/2, i]
int lb=i/2, ub=i;
while(lb+1!=ub){
m = lb/2 + ub/2;
if(x[m] < m)
lb=m;
else
ub = m;
}
if(A[m]!=Not_found&&A[m]==m)
return m;
else
return -1;
P*******b
发帖数: 1001
4
这个code有问题

【在 y*c 的大作中提到】
: i=1;
: while(A[i]!=Not_found&&A[i]<=i){
: if(A[i]==i)
: return i;
: i *= 2;
: }
: if(A[i]>i)
: return -1;
: // then binary search in [i/2, i]
: int lb=i/2, ub=i;

1 (共1页)
进入JobHunting版参与讨论
相关主题
O(N) sort integer arraytriplets 题怎么做比较高效?
amazon phone interviewAnother amazon interview questions
算法面试题array contains two integer that sum up to 7
问一道CareerCup里的题目算法题:两列找共同元素有O(n)的算法吗?
Google电话面试题目一个小公司面经
请教个面试题问个面试题
[合集] Google电话面试题目问一道老题
[算法] unsorted arraya[i] + b[j] = c[k] 的题有靠谱的答案不?
相关话题的讨论汇总
话题: array话题: found话题: ub话题: lb话题: index