no, that was not what we wanted to test.

这是一个开放性的问题,视答题人的程度,可以往下追。 既然连binary search都没有提及,也就不再追问下面的。 

最基本的回答是从头至尾逐个比较,发现相等的,即返回其位置。

达到这一步,我们再问,有可能改进效率吗?可能回答,是加一个不等比较

if (array[i] == n) 

  return i;

else if (array[i] > n)

  return -1;

这个不等比较是否能减少平均计算量而改进效率?

 

 

所有跟帖: 

回复:no, that was not what we wanted to test. -布衣之才- 给 布衣之才 发送悄悄话 布衣之才 的博客首页 (162 bytes) () 12/06/2012 postreply 21:23:22

请您先登陆,再发跟帖!