有没有比以下 c# 字数统计类的暴力实现更好的方法?
更新代码:抱歉!
/// <summary>
/// A word counting class.
/// </summary>
public class WordCounter
{
Dictionary<string, int> dictTest = new Dictionary<string, int> ();
/// <summary>
/// Enters a word and returns the current number of times that word was found.
/// </summary>
/// <param name="word">The word or string found.</param>
/// <returns>Count of times Found() was called with provided word.</returns>
public int Found ( string word )
{
int count = 1;
return dictTest.TryGetValue ( word, out count ) ? ++dictTest[word] : dictTest[word] = 1;
}
}
最佳答案
作为对 matt 的回应,Dictionary 基本上是一个具有泛型的 HashTable,因此查找是恒定时间(好吧,不完全是,但差不多)。
关于c# - 字计数器实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430319/