前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构 From Zero To Hero(六)

数据结构 From Zero To Hero(六)

作者头像
1ess
发布2021-10-29 16:15:43
2910
发布2021-10-29 16:15:43
举报
文章被收录于专栏:0x7c00的专栏

数据结构 From Zero To Hero(六)

發佈於 2021-02-27

本篇,我们介绍一种用于超快检索的数据结构 —— 哈希表(Hash Tables)或者称为字典(Dictionary)。

几乎所有的编程语言都有对应的实现,例如 Java 中的 HashMap、JavaScript 中的 Object、C# 中的 Dictionary。

First Non-repeated Character

代码语言:javascript
复制
private static char FirstNonRepeatedCharacter(string input)
{
    var map = new Dictionary<char, int>();
    foreach (var ch in input)
    {
        var count = map.Keys.Contains(ch) ? map[ch] : 0;
        map[ch] = count++;
    }

    foreach (var item in input)
    {
        if (map[item] == 1)
        {
            return item;
        }
    }

    return default(char);
}

Hash Collision

哈希函数会使用哈希算法根据输入计算出哈希表的 Value 实际存储的索引,可能发生不同输入计算出同一索引值的情况,这种我们就称之为哈希碰撞。

比较常用的有两种方式解决哈希碰撞:

  1. Chaining
  2. Open Addressing

自己构建一个哈希表

代码语言:javascript
复制
class HashTable<T1, T2>
{
    private class Entry
    {
        public Entry(T1 key, T2 value)
        {
            Key = key;
            Value = value;
        }
        public T1 Key;
        public T2 Value;
    }

    private LinkedList<Entry>[] enties;

    public HashTable(int capacity)
    {
        enties = new LinkedList<Entry>[capacity];
    }

    public void Put(T1 key, T2 value)
    {
        var entry = GetEntry(key);
        if (entry != null)
        {
            entry.Value = value;
            return;
        }

        GetOrCreateBucket(key).AddLast(new Entry(key, value));
    }

    public T2 Get(T1 key)
    {
        var entry = GetEntry(key);
        return entry == null ? default(T2) : entry.Value;
    }

    public void Remove(T1 key)
    {
        var entry = GetEntry(key);
        if (entry != null)
        {
            var bucket = GetBucket(key);
            bucket.Remove(entry);
        }
    }

    private LinkedList<Entry> GetBucket(T1 key)
    {
        return enties[Hash(key)];
    }

    private LinkedList<Entry> GetOrCreateBucket(T1 key)
    {
        var index = Hash(key);
        if (enties[index] == null)
        {
            enties[index] = new LinkedList<Entry>();
        }
        return enties[index];
    }

    private Entry GetEntry(T1 key)
    {
        var index = Hash(key);
        var bucket = enties[index];
        if (bucket != null)
        {
            foreach (var entry in bucket)
            {
                if (entry.Key.Equals(key))
                {
                    return entry;
                }
            }
        }
        return null;
    }

    private int Hash(T1 key)
    {
        return Math.Abs(key.GetHashCode() % enties.Length);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-02-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • First Non-repeated Character
  • Hash Collision
  • 自己构建一个哈希表
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档