leetcode题:706. 设计哈希映射(简单)

一、题目描述:706. 设计哈希映射(简单)

不使用任何内建的哈希表库设计一个哈希映射

具体地说,你的设计应该包含以下的功能

put(key, value):向哈希映射中插入(键,值)的数值对。如果键对应的值已经存在,更新这个值。
get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1。
remove(key):如果映射中存在这个键,删除这个数值对。

示例:

MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);          
hashMap.put(2, 2);         
hashMap.get(1);            // 返回 1
hashMap.get(3);            // 返回 -1 (未找到)
hashMap.put(2, 1);         // 更新已有的值
hashMap.get(2);            // 返回 1 
hashMap.remove(2);         // 删除键为2的数据
hashMap.get(2);            // 返回 -1 (未找到) 

注意:

所有的值都在 [1, 1000000]的范围内。
操作的总数目在[1, 10000]范围内。
不要使用内建的哈希库。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-hashmap
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二、解题思路

1、hash算法是取模,通过vector作为数组,解决冲突使用链表法。数组的初始长度max_key的值对性能影响比较大,数组过长,构造vector花的时间会比较长,数组过短会导致list长度较长,遍历时间趋向线性。

三、代码

class MyHashMap {
public:
    /** Initialize your data structure here. */
    MyHashMap() {
        max_key = 10000;
        _array = new vector<list<pair<int,int>>>(max_key,list<pair<int,int>>());
        
    }
    vector<list<pair<int,int>>> * _array;
    int max_key;
    
    /** value will always be non-negative. */
    void put(int key, int value) {
        int hash_key = key%max_key;
        list<pair<int,int>> & _list = (*_array)[hash_key];
        list<pair<int,int>>::iterator it = _list.begin();
        for(; it != _list.end(); it++)
        {
            if(it->first == key)
            {
                it->second = value;
                break;
            }
        }
        if(it == _list.end())
        {
            _list.push_back(make_pair(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) {
        int hash_key = key%max_key;
        list<pair<int,int>> & _list = (*_array)[hash_key];
        list<pair<int,int>>::iterator it = _list.begin();
        for(; it != _list.end(); it++)
        {
            if(it->first == key)
            {
                
                return it->second;
            }
        }
        return -1;

    }
    
    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    void remove(int key) {
        int hash_key = key%max_key;
        list<pair<int,int>> & _list = (*_array)[hash_key];
        list<pair<int,int>>::iterator it = _list.begin();
        for(; it != _list.end(); it++)
        {
            if(it->first == key)
            {
               _list.erase(it);
                return ;
            }
        }
    }
    ~MyHashMap()
    {
        delete _array;
        _array = NULL;
    }
};

/**
 * Your MyHashMap object will be instantiated and called as such:
 * MyHashMap* obj = new MyHashMap();
 * obj->put(key,value);
 * int param_2 = obj->get(key);
 * obj->remove(key);
 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot后端实操目是一项要求参与者使用Spring Boot框架进行后端开发的实践目。下面是一个简单的回答: Spring Boot是一个用于开发现代化的、基于Java的web应用程序的框架。它通过提供默认配置和约定大于配置的方式,简化了Spring应用程序的开发过程。因此,我们可以使用Spring Boot来快速搭建高效的后端应用。 在进行springboot后端实操目时,我们可以按照以下步骤进行: 1. 配置环境:首先,在本地开发环境中安装Java JDK和Spring Boot,并设置相关的环境变量。可以使用Eclipse、IntelliJ IDEA等集成开发环境来进行开发。 2. 创建项目:使用Spring Initializer来创建一个新的Spring Boot项目。在创建项目时,可以选择相应的依赖,如Web、JPA、Security等,根据实际需要选择适当的依赖。 3. 添加Controller:根据目要求的功能,在项目中创建相应的Controller类,并实现相应的接口方法。Controller类中可以使用注解来标识请求的路径和方法。 4. 实现业务逻辑:在Controller中,根据需求实现具体的业务逻辑。可以调用Service类来处理业务逻辑,并使用DAO类与数据库进行数据交互。 5. 配置路由:使用Spring Boot的路由配置,将请求映射到相应的Controller方法上。可以使用注解来表示请求路径、请求方法等。 6. 编写单元测试:为了保证代码的质量,编写相应的单元测试来验证功能的正确性。可以使用Spring Boot提供的测试框架和工具来进行单元测试。 7. 部署项目:完成开发后,可以使用Maven或Gradle来打包项目,并将其部署到服务器上。可以选择不同的部署方式,如Tomcat、Jetty等,来运行Spring Boot应用程序。 通过这样的步骤,我们可以完成一道springboot后端实操目。在实践中,我们还需要注意代码的可读性、可维护性和性能优化等方面,以提高开发效率和应用性能。最后,我们可以根据目要求进行代码的提交和演示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值