数据结构与算法[哈希表]


一、哈希表结构

1、什么是哈希表?

在之前队列和数组的学习过程中,我们可以发现队列操作数据方便,但是查找数据不方便,数组查找数据方便,但是操作数据不方便,哈希表便是队列和数组特性的结合,首先是数组组成的顺序结构便于数据查找,之后是几串链表组成的储存数据的结构,数组的每个成员都有指针指向链表的头节点。
在哈希表的结构中寻找一个数值,便要对应得到这两个数值,首先是在数组队列中的储存位置,然后是在链表结构中的位置,寻找数组中的储存位置首先要对数组的长度取余,得到的下标就是数值在数组中的储存位置,继而将数值添加到对应的链表中。

2、哈希表的结构

在这里插入图片描述

3、哈希表的实现(java语言实现)

//定义哈希表
public class HashMap
{
static class HashMap<String,Integer>
{
String key;
Integer value;
HashNode<String,Integer> next;//指向下一个节点
//链表申明
public HashNode(String key,Integer value)
{
this.key=key;
this.value=value;
}
}
private ArrayList<HashNode<String,Integer>> bucketArray;
private int numBuckets;//哈希数组的长度
private int size;
//
public HashMap()
{
bucketArray=new ArrayList<>();//左侧的数组
numBuckets = 10;
size =0;
for(int i=0;i<numBuckets;i++)
{
bucketArray.add(null);//給数组中的数值加上空的节点
}
}
}
## 求插入数值在数组中的位置
private int getBucketIndex(String key)
{
int hashCode = key.hashCode();
int index =hashCode % numBuckets;
return index;
}
//在哈希表中添加数字
public void add(String key,Integer value)
{
int bucketIndex = getBucketIndex(key);
HashNode<String,Integer>head = buketArray.get(bucketIndex);
while(head!=null)
{
if(head.key.equals(key))
{
head.value=value;
return;
}
head = head.next;
}
size++;
head = bucketArray.get(bucketIndex);
HashNode<String,Integer> newNode = new HashNode<String,Integer>(key,value);
newNode.next=head;
bucketArray.set(bucketIndex,newNode);
 if ((1.0 * size) / numBuckets >= 0.7) {
        ArrayList<HashNode<String, Integer>> temp = bucketArray;
        bucketArray = new ArrayList<>();
        numBuckets = 2 * numBuckets;
        size = 0;
        for (int i = 0; i < numBuckets; i++) {
            bucketArray.add(null);
        }
        for (HashNode<String, Integer> headNode : temp) {
            while(headNode != null) {
                add(headNode.key, headNode.value);
                headNode = headNode.next;
}
//通过关键字找到对应数组的函数
public Integer get(String key) {
    int bucketIndex = getBucketIndex(key);
    HashNode<String, Integer> head = bucketArray.get(bucketIndex);
    while (head != null) {
        if (head.key.equals(key)) {
            return head.value;
        }
        head = head.next;
    }
    return null;
}
//在哈希表中寻找数值并返回数值位置
public Integer remove(String key) {
    int bucketIndex = getBucketIndex(key);//定义数组大小
    HashNode<String, Integer> head = bucketArray.get(bucketIndex);//
    HashNode<String, Integer> prev = null;//头节点置空
    while (head != null) 
    {
        if (head.key.equals(key))
            break;
        prev = head;
        head = head.next;
    }
    if (head == null) {
        return null;
    }
    size--;
    if (prev != null) {
        prev.next = head.next;
    } else {
        bucketArray.set(bucketIndex, head.next);
    }
    return head.value;
}

接下来是数据结构知识点的实际应用
leetcode-1
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现
解题思路:
返回两个相加的数值下标,每次在存入哈希表时,查看哈希表中是否有数值与该数值相加等于和即可。

class Solution
{
public:
vector<int> twoSum(vector<int>&nums,int target)
{
//定义哈希表
unfordered_map<int,int>hashtable;
for(int i=0;i<nums.size();++i)
{
auto it = hashtable.find(target-nums[i]);
if(it!=hashtable.end())
{
return(it->second,i);
}
hashtable[nums[i]]=i;
}
return {};
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值