c语言——uthash使用
参考:https://troydhanson.github.io/uthash/userguide.html
https://blog.csdn.net/lovemust/article/details/105208408

参考练习:https://leetcode-cn.com/circle/article/y7G2Gq/
1. 两数之和
struct hashTable {
int key;
int val;
UT_hash_handle hh;
};
struct hashTable *hashtable;
struct hashTable* FindVal(int ikey)
{
struct hashTable* tmp;
HASH_FIND_INT(hashtable, &ikey, tmp);
return tmp;
}
void AddNode(int ikey, int ival)
{
struct hashTable* it = FindVal(ikey);
if (it == NULL) {
struct hashTable* tmp = (struct hashTable *)malloc(sizeof(struct hashTable));
tmp->key = ikey;
tmp->val = ival;
HASH_ADD_INT(hashtable, key, tmp);
} else {
it->val = ival;
}
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{
hashtable = NULL;
for (int i = 0; i < numsSize; i++) {
struct hashTable *it = FindVal(target - nums[i]);
if (it != NULL) {
int *res = (int *)malloc(sizeof(int) * 2);
*returnSize = 2;
res[0] = it->val;
res[1] = i;
return res;
}
AddNode(nums[i], i);
}
*returnSize = 0;
return NULL;
}
49. 字母异位词分组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <uthash\src\uthash.h> #define MAXLENGTH 10000
struct hashTable {
char keyStr[MAXLENGTH]; // key值为排序后的字符串
int id; // 记录在res中的位置
int count; // 记录分组中元素的个数
UT_hash_handle hh;
}; int com(const void *a_, const void *b_){
char *a = (char *)a_;
char *b = (char *)b_;
return *a - *b;
} char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){
if (strsSize == 0) return NULL;
char ***res = (char ***)malloc(sizeof(char **) * strsSize);
*returnColumnSizes = (int *)malloc(sizeof(int) * strsSize);
*returnSize = 0;
struct hashTable *hashtable = NULL;
for(int i=0; i<strsSize; i++){
int len = strlen(strs[i]);
char tmp[len+1];
memset(tmp, 0, sizeof(char) * (len+1));
memcpy(tmp, strs[i], sizeof(char) * len);
// qsort tmp
qsort(tmp, len, sizeof(char), com);
struct hashTable *tmpHash;
HASH_FIND_STR(hashtable, tmp, tmpHash);
if(tmpHash == NULL) {
// HASH表中没有
tmpHash = (struct hashTable *)malloc(sizeof(struct hashTable));
memset(tmpHash->keyStr, 0, sizeof(char) * MAXLENGTH);
memcpy(tmpHash->keyStr, tmp, sizeof(char) * len);
tmpHash->id = *returnSize;
tmpHash->count = 1;
HASH_ADD_STR(hashtable, keyStr, tmpHash); res[*returnSize] = (char **)malloc(sizeof(char *) * strsSize);
res[*returnSize][tmpHash->count-1] = (char *)malloc(sizeof(char) * (len + 1)); memset(res[*returnSize][tmpHash->count-1], 0, sizeof(char) * (len + 1));
memcpy(res[*returnSize][tmpHash->count-1], strs[i], sizeof(char) * len);
(*returnColumnSizes)[*returnSize] = tmpHash->count;
(*returnSize)++;
}
else {
// HASH表中有记录
res[tmpHash->id][tmpHash->count] = (char *)malloc(sizeof(char) * (len + 1));
memset(res[tmpHash->id][tmpHash->count], 0, sizeof(char) * (len + 1));
memcpy(res[tmpHash->id][tmpHash->count], strs[i], sizeof(char) * len);
(*returnColumnSizes)[tmpHash->id] = tmpHash->count + 1;
tmpHash->count = tmpHash->count + 1; }
} return res; }
代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <uthash.h> typedef struct MyStruct {
int key;
int val1;
int val2;
UT_hash_handle hh;
} Hash; int cmpFunc(const void *_a, const void *_b)
{
Hash *a = (Hash *)_a;
Hash *b = (Hash *)_b;
return a->val1 - b->val1;
} int main(int argc, const char *argv[])
{ Hash *hashTbl = NULL;
Hash *hashNode = NULL;
Hash *hashTmp = NULL; for (int i = 0; i < 10; i++) {
hashNode = (Hash *)malloc(sizeof(Hash));
hashNode->key = i;
hashNode->val1 = 1;
hashNode->val2 = 2;
HASH_ADD_INT(hashTbl, key, hashNode);
}
int a = 8;
HASH_FIND_INT(hashTbl, &a, hashTmp);
printf("%d\n", hashTmp->val1);
hashTmp->val1 = 100; for (Hash *s = hashTbl; s != NULL; s = s->hh.next) {
printf("%d,", s->val1);
}
printf("\n");
HASH_SORT(hashTbl, cmpFunc);
for (Hash *s = hashTbl; s != NULL; s = s->hh.next) {
printf("%d,", s->val1);
} printf("\n%d",HASH_COUNT(hashTbl));
return 0;
}
面试题 16.02. 单词频率
typedef struct {
char name[20];
int count;
UT_hash_handle hh;
} WordsFrequency;
WordsFrequency *g_word = NULL;
void AddWord(char *iname)
{
WordsFrequency *tmp = NULL;
HASH_FIND_STR(g_word, iname, tmp);
if (tmp == NULL) {
tmp = malloc(sizeof(WordsFrequency));
// 下面三种方式都可以
// memcpy(tmp->name, iname, strlen(iname) + 1);
// strcpy(tmp->name, iname);
sprintf(tmp->name, "%s", iname);
tmp->count = 1;
HASH_ADD_STR(g_word, name, tmp);
} else {
tmp->count++;
}
}
WordsFrequency* wordsFrequencyCreate(char** book, int bookSize)
{
for (int i = 0; i < bookSize; i++) {
AddWord(book[i]);
}
return NULL;
}
int wordsFrequencyGet(WordsFrequency* obj, char* word)
{
WordsFrequency *tmp = NULL;
HASH_FIND_STR(g_word, word, tmp);
if (tmp == NULL) {
return 0;
} else {
return tmp->count;
}
}
void wordsFrequencyFree(WordsFrequency* obj)
{
HASH_CLEAR(hh, g_word);
/*
WordsFrequency *cur = NULL;
WordsFrequency *tmp = NULL;
HASH_ITER(hh, g_word, cur, tmp) {
if (cur != NULL) {
HASH_DEL(g_word, cur);
free(cur);
}
}
*/
}
347. 前 K 个高频元素
typedef struct {
int key;
int value;
UT_hash_handle hh;
} hashTable;
hashTable *g_hash = NULL;
hashTable* FindNode(int ikey)
{
hashTable *tmp = NULL;
HASH_FIND_INT(g_hash, &ikey, tmp);
return tmp;
}
void AddNode(int ikey)
{
hashTable *tmp = FindNode(ikey);
if (tmp == NULL) {
tmp = malloc(sizeof(hashTable));
tmp->key = ikey;
tmp->value = 1;
HASH_ADD_INT(g_hash, key, tmp);
} else {
tmp->value++;
}
}
int Cmp(hashTable *a, hashTable *b)
{
return b->value - a->value;
}
int Sort()
{
HASH_SORT(g_hash, Cmp);
return 0;
}
int* topKFrequent(int* nums, int numsSize, int k, int* returnSize)
{
*returnSize = 0;
if (nums == NULL || k > numsSize) {
return NULL;
}
for (int i = 0; i < numsSize; i++) {
AddNode(nums[i]);
}
int *res = malloc(sizeof(int) * k);
*returnSize = k;
// 排序
Sort();
// 遍历
hashTable *curr, *tmp;
int count = 0;
HASH_ITER(hh, g_hash, curr, tmp) {
res[count++] = curr->key;
if (count == k) {
break;
}
}
// 释放内存
HASH_ITER(hh, g_hash, curr, tmp) {
HASH_DEL(g_hash, curr);
free(curr);
}
return res;
}
692. 前K个高频单词
typedef struct {
char key[20];
int value;
UT_hash_handle hh;
} hashTble;
hashTble *g_hash = NULL;
hashTble* FindNode(char *w)
{
hashTble *tmp = NULL;
HASH_FIND_STR(g_hash, w, tmp);
return tmp;
}
void AddNode(char *w)
{
hashTble *tmp = FindNode(w);
if (tmp == NULL) {
tmp = malloc(sizeof(hashTble));
memcpy(tmp->key, w, strlen(w) + 1);
tmp->value = 1;
HASH_ADD_STR(g_hash, key, tmp);
} else {
tmp->value++;
}
}
/*
* 两次排序:
* 1.先排序字母
* 2.再排序出现次数
*/
int CmpAlpha(hashTble *a, hashTble *b)
{
return strcmp(a->key, b->key);
}
void SortAlpha()
{
HASH_SORT(g_hash, CmpAlpha);
}
int CmpCount(hashTble *a, hashTble *b)
{
return b->value - a->value;
}
void SortCount()
{
HASH_SORT(g_hash, CmpCount);
}
char ** topKFrequent(char ** words, int wordsSize, int k, int* returnSize)
{
*returnSize = 0;
if (words == NULL || k > wordsSize) {
return NULL;
}
*returnSize = k;
char **res = malloc(sizeof(char *) * k);
// 添加节点
for (int i = 0; i < wordsSize; i++) {
AddNode(words[i]);
}
// 排序
SortAlpha();
SortCount();
// 遍历
hashTble *cur, *tmp;
int count = 0;
HASH_ITER(hh, g_hash, cur, tmp) {
// 注意char ** 里面一层的内存要先申请,才能使用
res[count] = malloc(sizeof(char) * (strlen(cur->key) + 1));
strcpy(res[count++], cur->key);
if (count == k) {
break;
}
}
// 释放内存
HASH_ITER(hh, g_hash, cur, tmp) {
HASH_DEL(g_hash, cur);
free(cur);
}
return res;
}
c语言——uthash使用的更多相关文章
- C开源hash项目uthash
uthash 是C的比较优秀的开源代码,它实现了常见的hash操作函数,例如查找.插入.删除等.该套开源代码采用宏的方式实现hash函数的相关功能,支持C语言的任意数据结构最为key值,甚至可以采用多 ...
- C 语言资源大全中文版
C 语言资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-c 是 koz.ross 发起维护的 C 语言资源列表,内容包括了: ...
- C :uthash
参考: [1] uthash | 学步园 [2] 源码 [3] 官方文档 [4] [5] 一.哈希表的概念及作用 在一般的线性表或者树中,我们所储存的值写它的存储位置的关系是随机的.因此,在查找过程中 ...
- C语言 · 高精度加法
问题描述 输入两个整数a和b,输出这两个整数的和.a和b都不超过100位. 算法描述 由于a和b都比较大,所以不能直接使用语言中的标准数据类型来存储.对于这种问题,一般使用数组来处理. 定义一个数组A ...
- Windows server 2012 添加中文语言包(英文转为中文)(离线)
Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...
- iOS开发系列--Swift语言
概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...
- C语言 · Anagrams问题
问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...
- C语言 · 字符转对比
问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...
- JAVA语言中的修饰符
JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...
随机推荐
- 阅读笔记——长文本匹配《Matching Article Pairs with Graphical Decomposition and Convolutions》
论文题目:Matching Article Pairs with Graphical Decomposition and Convolutions 发表情况:ACL2019 腾讯PCG小组 模型简介 ...
- docker简单介绍。
docker是啥? 一.概念? // 和运维有关的工具,和开发没有很大的关系.只需要去调试项目,将项目运行更迅速. 二.作用? 1.只需要关心项目的编写和调试,不需要关心具体的项目需要运行在哪里,并且 ...
- plsql 将游标读取到table中
-- 将游标中的数据 读取到table中 根据部门编号获得emp所有信息. declare cursor c(no emp.deptno%type)is select * from emp where ...
- Ubuntu 配置数据库开发环境(mysql oracle mssqlserver sybase)
1.mysql sudo apt-get install libmysql++-dev //mysql连接库 2.ms sql server/sybase ./configure --prefix=/ ...
- Argo 安装和 workflow 实例配置文件解析
一.Argo 安装配置 1.1 Argo 安装 $ kubectl create ns argo $ kubectl apply -n argo -f https://raw.githubuserco ...
- winSCP上传文件到服务器失败,提示permission denied,返回码3
1.查看sftp服务在你服务器的路径 cat /etc/ssh/sshd_config | grep sftp 2.在winSCP进行连接设置 设置好你的主机名.端口.用户名和密码,然后点击高级,点击 ...
- Android 使用签名的好处【转】
感谢大佬:https://zhidao.baidu.com/question/360127490062917572.html 平时我们的程序可以在模拟器上安装并运行,是因为在应用程序开发期间,由于是以 ...
- 判断js对象是否为空
let _isEmptyObj = function(obj) { for(var key in obj) { return false; } return true; }
- ittun.com的使用方法
[如果这篇文章对你有所作用,请加关注哦!] 步骤一: 进入官网http://ittun.com/ Windows 64位下载http://ittun.com/upload/17.2/ittun_win ...
- Vue项目中实现文件下载到本地的功能
公司业务需求,我需要实现一个合同模板,自定义输入内容后生成合同随后导出下载合同.(自定义部分用到的是) 为了实现这个文件下载到本地的功能,真的是废了九牛二虎之力,以至于差点放弃(主要还是自己菜).刚开 ...