淘客熙熙

主题:求教大家一个算法问题 -- looklook

共:💬24 🌺10 新:
全看树展主题 · 分页首页 上页
/ 2
下页 末页
家园 hash是对的。 但是, 你的contains()如何最快实现呢!
家园 這不是pigeon hole sorting的算法嗎:)
家园 的确,浮点数的情况比较麻烦,可能还是先排序快...

要建一个万能hash table也要费不少代价。

家园 Variation of the classic issue.

There are lots of variation of the classic issue.

One variation is: there is a character sequence composed of 26 English alphabets: a-z, assuming all lower case. How to find the duplicate character in the sequence if there is any?

The solution is to use an array of 26 to hold a-z. If there is an array element being written twice, then that\'s the duplicate.

As a matter of fact, this is one of the questions in a Microsoft interview.

bigbug

家园 There are always two solutions for the classic issue.

As for contains() implementation, check out Sun's JDK implementation. I believe it's O(1) as the Javadoc declared.

Well, there are always two solutions for the classic issue. One is the solution you mentioned above, and the other is the one I gave.

bigbug

家园 Quick Sort的最差情况是平方级别的

平均情况为O(n log n)

家园 not the most efficient method

you sure this is the solution for msft interview question?

if the sequence is continuous (but one missing/duplicating) and bounded, the solution is to take the summation and then get the one missing/duplicating by comparing with the summation of the whole continuous sequence. (actually, i guess one more function, e.g. summation of squres is needed to find the duplicating/missing couple).

家园 这就相当于SQL中最常用的方法啊,select *,count(*) as N having N=2;
家园 Yes, it's the solution for Microsoft interview question.

Yes, I am sure it's the solution for Microsoft interview question because I had it before and confirmed with the interviewer.

全看树展主题 · 分页首页 上页
/ 2
下页 末页


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河