当前位置:网站首页>< 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 .
边栏推荐
- Utnet hybrid transformer for medical image segmentation
- 微策生物IPO过会:年营收12.6亿 睿泓投资与耀合医药是股东
- Interview secrets are widely distributed, and the exclusive secrets of editing, testing and learning are leaked?!
- NFT 的 10 种实际用途
- 汉字风格迁移篇---对抗性区分域适应(L1)Adversarial Discriminative Domain Adaptation
- West test Shenzhen Stock Exchange listing: annual revenue of 240million, fund-raising of 900million, market value of 4.7 billion
- Hard deduction SQL statement exercises, wrong question records
- Blocking queue
- 一篇文章看懂JS执行上下文
- SLAM综述阅读笔记四:A Survey on Deep Learning for Localization and Mapping: Towards the Age of Spatial 2020
猜你喜欢

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

West test Shenzhen Stock Exchange listing: annual revenue of 240million, fund-raising of 900million, market value of 4.7 billion

Pure C handwriting thread pool

井贤栋等蚂蚁集团高管不再担任阿里合伙人 确保独立决策

文旅数藏 | 用艺术的方式游云南

PROFINET 模拟器使用教程

Unity3D学习笔记10——纹理数组

【笔记】逻辑斯蒂回归
![[training day3] section [greed] [two points]](/img/4f/4130a1ade0ac0003adeddca780ff14.png)
[training day3] section [greed] [two points]

592. 分数加减运算
随机推荐
[training day4] anticipating [expected DP]
Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China
codeforces 1708E - DFS Trees
Redis implements the browsing history module
Alibaba's latest equity exposure: Softbank holds 23.9% and caichongxin holds 1.4%
Document translation__ Salt and pepper image denoising based on adaptive total variation L1 regularization
致尚科技IPO过会:年营收6亿 应收账款账面价值2.7亿
基于预训练模型的多标签专利分类研究
Slam overview Reading Note 4: a survey on deep learning for localization and mapping: towards the age of spatial 2020
Schematic diagram of C measuring tool
力扣SQL语句习题,错题记录
Chapter 3 business function development (add clues and remarks, and automatically refresh the added content)
基于招聘广告的岗位人才需求分析框架构建与实证研究
开源版思源怎么私有部署
c语言分层理解(c语言数组)
融合迁移学习与文本增强的中文成语隐喻知识识别与关联研究
Mining enterprise association based on Enterprise Knowledge Map
Getting started for beginners: build your own blog with WordPress
DVWA全级别通关教程
西测测试深交所上市:年营收2.4亿募资9亿 市值47亿