我最近發現了一句這樣的問題:以下用於查找不同字符串的算法是否有效?
"Given an array of strings, return the number of distinct strings in that array."
我想出了這個解決方案:
1. Get number_of_strings, which equals the number of strings in the input array
2. Get number_of_non_redundant, which equals the length of the input array cast as a set
3. Return 2 times number_of_non_redundant - number_of_strings
所以,我的問題是,確實爲所有數據集,該算法的工作?
2次non_redundant - num_strings來自哪裏?不僅僅是這套作品的長度? – 2012-08-15 17:40:52
是不是'number_of_non_redundant'已經是答案? – Chris 2012-08-15 17:41:12