问题描述
我是prepping采访,和一些明显的面试问题,如在字符串中的字符计数频率涉及到把所有的字符转换成一个哈希表/字典,以便得到O(n)的运行时间为算法。我的问题是,什么是性能通过点击的containsKey
和 TryGetValue
来检查,看看是否有键已经被插入到哈希表?我还可以有一个O(n)的算法,这样的使用问题的containsKey
或 TryGetValue
?
I'm prepping for interviews, and some obvious interview questions such as counting frequency of characters in a string involve putting all of the characters into a Hashtable/Dictionary in order to get O(n) runtime for the algorithm. My question is, what is the performance hit by using ContainsKey
and TryGetValue
to check to see if a key has already been inserted into the Hashtable? Can I still have an O(n) algorithm for problems like these that use ContainsKey
or TryGetValue
?
推荐答案
假设一个好的哈希没有太多的冲突,每个这些都是O(1)操作。
Assuming a good hash without too many collisions, each of those are O(1) operations.
至于如何将这些操作的工作......我建议你阅读起来哈希表的。
As for how those operations work... I suggest you read up on hash tables.
这篇关于什么是的containsKey和TryGetValue的表现呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!