当前位置:网站首页>< C> C language hash table usage
< C> C language hash table usage
2022-07-27 14:24:00 【Remember to look up at the stars】
C Language hash table usage example
One 、 The header file
#include "uthash.h"
Two 、API
Structure form
struct HashTable_t {
int key;
int value;
UT_hash_handle hh;
}
- key The type of can be char *、char []、int、void* Four kinds of , value It can be of any type .
- UT_hash_handle The structure is hash API Use , You don't have to pay attention to .
HASH_ADD_INT()
add to int Parameters to hash table ,HASH_ADD_INT(hash, key, value)
HASH_FIND_INT()
Look up... From the hash table int Parameters ,HASH_FIND_INT(hash, &key, value)
And HASH_ADD_INT Different ,FIND API Medium key Need to get the address .
HASH_DEL()
Remove node from hash table .
HASH_COUNT()
Count the number of nodes in the hash table .
3、 ... and 、 Sample code
The code is leetcode subject , Sum of two numbers
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6
Output :[0,1]
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/two-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
/** * Note: The returned array must be malloced, assume caller calls free(). */
struct hashTable {
//HASH Table structure definition
int key;
int value;
UT_hash_handle hh;
};
struct hashTable *Hash; // Hash table global scalar
struct hashTable *find(int key) // Find out if there is... In the hash table
{
struct hashTable *tmp = (struct hashTable *)malloc(sizeof(struct hashTable));
HASH_FIND_INT(Hash, &key, tmp);
if(tmp)
return tmp;
else
return NULL;
}
void insert(int key, int value)
{
struct hashTable *tmp = find(key); // Before insertion , Find out if there are the same key
if(tmp == NULL) {
// inequality
struct hashTable *node = (struct hashTable *)malloc(sizeof(struct hashTable));
node->key = key;
node->value = value;
HASH_ADD_INT(Hash, key, node); // Create a new hash node , insert data .
} else
tmp->value = value // There is the same , Update data .
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int *result = (int *)malloc(sizeof(int) * 2);
struct hashTable *ret = NULL;
Hash = NULL;
for(int i = 0 ; i < numsSize; i++){
ret = find(target - nums[i]);
if(ret) {
result[0] = ret->value;
result[1] = i;
*returnSize = 2;
return result;
}
insert(nums[i], i);
}
*returnSize = 0;
return NULL;
}
summary
1、 be familiar with C Use of Library hash table
2、 Sum of two numbers , How to convert to hash table . The same array that needs to be iterated , Consider using a hash table , Reduce time complexity , But the use of hash table increases the space complexity . Sacrifice space , Speed up .
边栏推荐
- How to make computers have public IP
- Document translation__ Salt and pepper image denoising based on adaptive total variation L1 regularization
- Chapter3 data analysis of the U.S. general election gold offering project
- Named entity recognition of Chinese electronic medical records based on Roberta WwM dynamic fusion model
- Cognition -- classic of the road to success of hardware engineers
- Arduino+ze08-ch2o formaldehyde module, output formaldehyde content
- Hdu1422 revisits the world cup [DP]
- JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链
- Ncnn compilation and use pnnx compilation and use
- WPF visifire.charts4.6.1 tutorial with source code
猜你喜欢

Excellent basic methods of URL parsing using C language

Realize the basic operations such as the establishment, insertion, deletion and search of linear tables based on C language

万字详解 Google Play 上架应用标准包格式 AAB

codeforces 1708E - DFS Trees

Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China

Interview eight part essay · TCP protocol

【论文精读】Grounded Language-Image Pre-training(GLIP)
![[related contents of multithreading]](/img/2d/c8bde21f13a5305ba54e9b52bd1e89.png)
[related contents of multithreading]

基于预训练模型的多标签专利分类研究

面向不平衡数据的电子病历自动分类研究
随机推荐
Named entity recognition of Chinese electronic medical records based on Roberta WwM dynamic fusion model
Good architecture is evolved, not designed
GoPro access - control and preview GoPro according to GoPro official document /demo
Motion attitude control system of DOF pan tilt based on stm32
Recursive method to realize the greatest common divisor
正掩码、反掩码、通配符
Mining enterprise association based on Enterprise Knowledge Map
Document translation__ Salt and pepper image denoising based on adaptive total variation L1 regularization
知识关联视角下金融证券知识图谱构建与相关股票发现
codeforces 1708E - DFS Trees
平板模切机
STM32——电容触摸按键实验
[training day4] sequence transformation [thinking]
文献翻译__基于自适应全变差L1正则化的椒盐图像去噪
2022 Niuke multi School II_ E I
YOLOX改进之一:添加CBAM、SE、ECA注意力机制
RTL8762DK 环境搭建(一)
基于RoBERTa-wwm动态融合模型的中文电子病历命名实体识别
watch VS watchEffect
Positive mask, negative mask, wildcard