c# - 字计数器实现

标签 c# algorithm

有没有比以下 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/

相关文章:

c# - 如何正确实现多个 Vector3.Lerps 串联?

c# - 具有混合对象类型的嵌套 XAML TreeView 结构

c# - 如何使用带有 MVVM 的 WPF 应用程序中的 FolderBrowserDialog

algorithm - 使用迭代替换查找多次重复的运行时间

algorithm - 最坏情况时间复杂度分析伪代码

algorithm - 在另一个时间段内迭代离散时间段

确定两个不同频率信号之间有效 "phase difference"的算法?

c# - 使用 MVVM 在 Silverlight 3 DataGrid 中编辑新添加的行

c# - 将 C# 函数移动到表达式以在 Entity Framework/SQL Select 中使用

寻找子集组合以实现给定总和同时保持成本最低的算法