搜索文档中出现次数最多的单词的最佳方法(算法)是什么?
查找文档中出现次数最多的单词可以通过简单的 O(n) 时间复杂度完成直方图 http://en.wikipedia.org/wiki/Histogram[基于哈希]:
histogram <- new map<String,int>
for each word in document:
if word in histogram:
histogram[word] <- histogram[word] + 1
else:
histogram[word] <- 1
max <- 0
maxWord<- ""
for each word in histogram:
if histogram[word] > max:
max <- histogram[word]
maxWord <- word
return maxWord
这是 O(n) 解决方案,并且由于该问题显然是 Omega(n) 问题,因此它在以下方面是最优的大O表示法 http://en.wikipedia.org/wiki/Big_O_notation.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)