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

编程入门 行业动态 更新时间:2024-10-07 18:25:11
本文介绍了给定一个文件,找到十个最经常存在的话尽可能有效地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这显然是一个面试问题(发现它在面试问题的集合),但即使它不是它的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} 来消除计数。

更多推荐

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

本文发布于:2023-11-29 09:24:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有效地   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!