给定一个文件,找到十个最经常存在的话尽可能有效地的话、有效地、存在、文件

由网友(吟思)分享简介:这显然是一个面试问题(发现它在面试问题的集合),但即使它不是它的pretty的凉爽。 This is apparently an interview question (found it in a collection of interview questions), but even if it's not it'...

这显然是一个面试问题(发现它在面试问题的集合),但即使它不是它的pretty的凉爽。

This is apparently an interview question (found it in a collection of interview questions), but even if it's not it's pretty cool.

我们被告知要有效做到这一点的所有复杂性的措施。我想创造这样的词语映射到其频率一个HashMap的。这将是O(n)的时间和空间复杂度,但是因为可能有很多的话,我们不能想当然地认为我们可以存储的一切记忆。

We are told to do this efficiently on all complexity measures. I thought of creating a HashMap that maps the words to their frequency. That would be O(n) in time and space complexity, but since there may be lots of words we cannot assume that we can store everything in memory.

我要补充一点,没有什么问题说的话不能被存储在内存中,但如果是这样的话?如果不是的话,那么这个问题似乎并不困难。

I must add that nothing in the question says that the words cannot be stored in memory, but what if that were the case? If that's not the case, then the question does not seem as challenging.

推荐答案

优化我自己的时间:

sort file | uniq -c | sort -nr | head -10

随后可能的awk'{打印$ 2} 来消除计数。

阅读全文

相关推荐

最新文章