706. Design HashMap

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求
Design a HashMap without using any built-in hash table libraries.
To be specific, your design should include these functions:
这里写图片描述
这里写图片描述
设计并实现一个HashMap。

2,题目思路
比较简单的办法,是直接定义vector来实现这样的HashMap,利用vector容器的特性来实现。不过这种办法的效率比较低。(加上前面的cin优化之后会好一些)
而另外一种办法,则是利用自己定义的链表来存储这样的节点,并利用mod运算来减少空间的存储:即创建长度适当的vector,其中的元素类型为Node*类型,即元素类型为单链表类型,在保证搜索效率的基础的上也可以减少空间存储的消耗。而删除元素和添加元素也是用到了链表的特性罢了。

3,程序源码
方法1:(直接利用vector)

static int pr = []() { 
    std::ios::sync_with_stdio(false); 
    cin.tie(NULL);  
    return 0; 
}();

class MyHashMap {
public:
    /** Initialize your data structure here. */
    MyHashMap() {}

    /** value will always be non-negative. */
    void put(int key, int value) {
        HashMap[key] = value;
    }

    /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
    int get(int key) {
        return HashMap[key];
    }

    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    void remove(int key) {
        HashMap[key] = -1;
    }
private:
    vector<int> HashMap = vector<int> (1000001, -1);
};

注:
脑子有点昏了,以为vector<int> HashMap = vector<int> (1000001, -1);是创建一个二维向量,其实就是简单的一维向量的初始化的定义方式罢了。跟:

vector<int> HashMap (100001, -1);

效果是一样的,但是在类中不能这样直接的进行定义,需要先声明后,再进行初始化,就像上面的代码的方式那样。
方法2:(利用链表)

static int pr = []() { 
    std::ios::sync_with_stdio(false); 
    cin.tie(NULL);  
    return 0; 
}();

class MyHashMap {
public:
    /** Initialize your data structure here. */
    MyHashMap() {
        slots = 3333;   //减少空间的占用
        hm = vector<mapNode*>(slots, NULL);//相当于创建了3333个单链表
    }

    /** value will always be non-negative. */
    void put(int key, int value) {
        int slot = key % slots;
        for(auto ptr = hm[slot];ptr != NULL;ptr = ptr->next)
        {
            if(ptr->key == key) //找到了节点,更新值
            {
                ptr->val = value;
                return;
            }
        }
        //没有找到(插入到第一个节点前面)
        auto tmp = hm[slot];    //第一个节点
        hm[slot] = new mapNode(key, value);
        hm[slot]->next = tmp;
        return;

    }

    /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
    int get(int key) {
        int slot = key % slots;
        for(auto ptr = hm[slot];ptr != NULL;ptr = ptr->next)
            if(ptr->key == key) return ptr->val;
        return -1;
    }

    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    void remove(int key) {
        int slot = key % slots;
        if(hm[slot] == NULL)    return;
        else if(hm[slot]->key == key)
        {
            hm[slot] = hm[slot]->next;
            return;
        }
        else
        {
            for(auto ptr= hm[slot], pre = hm[slot];ptr != NULL;pre = ptr, ptr = ptr->next)
            {
                if(ptr->key == key)
                {
                    pre->next = ptr->next;
                    return;
                }
            }
        }
    }

private:
    struct mapNode {
        int key;
        int val;
        mapNode* next;
        mapNode(int k, int v) : key(k), val(v), next(NULL) {};
    };

    vector<mapNode*> hm;
    int slots;

};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本教程为授权出品教程1) 优秀的程序应该是这样的:阅读时,感觉很优雅;新增功能时,感觉很轻松;运行时,感觉很快速,这就需要设计模式支撑2) 设计模式包含了大量的编程思想,讲授和真正掌握并不容易,网上的设计模式课程不少,大多讲解的比较晦涩,没有真实的应用场景和框架源码支撑,学习后,只知其形,不知其神。就会造成这样结果: 知道各种设计模式,但是不知道怎么使用到真实项目。本课程针对上述问题,有针对性的进行了升级 (1) 授课方式采用 图解+框架源码分析的方式,让课程生动有趣好理解 (2) 系统全面的讲解了设计模式,包括 设计模式七大原则、UML类图-类的六大关系、23种设计模式及其分类,比如 单例模式的8种实现方式、工厂模式的3种实现方式、适配器模式的3种实现、代理模式的3种方式、深拷贝等3) 如果你想写出规范、漂亮的程序,就花时间来学习下设计模式吧课程内容和目标本课程是使用Java来讲解设计模式,考虑到设计模式比较抽象,授课采用 图解+框架源码分析的方式1) 内容包括:设计模式七大原则(单一职责、接口隔离、依赖倒转、里氏替换、开闭原则、迪米特法则、合成复用)、UML类图(类的依赖、泛化和实现、类的关联、聚合和组合) 23种设计模式包括:创建型模式:单例模式(8种实现)、抽象工厂模式、原型模式、建造者模式、工厂模式。结构型模式:适配器模式(3种实现)、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式(3种实现)。行为型模式:模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式)。2) 学习目标:通过学习,学员能掌握主流设计模式,规范编程风格,提高优化程序结构和效率的能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值