Redian新闻
>
谁帮俺看看OSK,抄的底,卖还是留。
avatar
谁帮俺看看OSK,抄的底,卖还是留。# Stock
j*e
1
Given an array of sorted numbers, a number to search for and a condition
such as GreaterThanEquals, LessThan etc, return the match if present and the
index. Please use simple linear search, can not use binary search tree. The
array is either in descending order or in ascending order.
这是道超级简单的面试题吗?还是到难题?有什么TRICKY的地方?
刷题大牛们,分析一下。
多谢。
avatar
g*u
2
不回答,俺就卖一半了。
avatar
j*e
3
是因为有不同种的操作符,如果一个操作一个函数,几个函数会重合很多,所以需要找
到一个方式,写尽可能少的CODE吗,比如说一个函数就覆盖所有的操作。
avatar
j*e
4
有没有大牛吱一声?
这道题看似简单,实则很容易产生很多重复的CODE。因为LESS THAN 和GREATER THAN是
对称的,同事,数组又有升序和降序两种情况,组合起来是4种情况,很容易产生重复
CODE.
有什么好的方法没有?
avatar
a*n
5
估计是在一开始判断升序或降序, set a flag, 然后直接用greaterthan or
lessthan

the
The

【在 j*****e 的大作中提到】
: Given an array of sorted numbers, a number to search for and a condition
: such as GreaterThanEquals, LessThan etc, return the match if present and the
: index. Please use simple linear search, can not use binary search tree. The
: array is either in descending order or in ascending order.
: 这是道超级简单的面试题吗?还是到难题?有什么TRICKY的地方?
: 刷题大牛们,分析一下。
: 多谢。

avatar
l*i
6
Use functor, example std::greater
avatar
j*e
7
我试着用C写了一下。也是先判断升序降序,然后是分LESS THAN, LESS THAN EQUAL,
GREATER THAN, GREATER THAN EQUAL, EQUAL, 一共5中情况,然后每种情况的区别主
要是>,还是CODE显得很繁琐。
哪位大牛能举个FUNCTOR的例子。
多谢。
//升序
case( LessThan ):
{
i = 0;
while ( ( items[ i ] < key ) )
i++;
if( ( i == num_elems ) || ( ( elem[ i ] >= key ) && ( i == 0 ) ) )
result= NotExist;
else
{
*index = i - 1;
result = FoundResult;
}
break;
}
//降序 GREATER THAN
case( GreaterThan ):
{
i = 0;
while ( ( elems[ i ] < key ) )
i++;
if( ( i == num_elems ) || ( ( elems[ i ] == key ) && ( i == 0 ) ) )
result= NotExist;
else
{
*index = i;
result = Found;
}
break;
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。