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--------------------------------------- ...
随机推荐
- redis一主两从搭建
一主两从搭建: 主配: daemonize yes port 6379 logfile ./redis6379.log dir ./ bind 10.131.156.170 从1配: daemoniz ...
- SuperPoint: Self-Supervised Interest Point Detection and Description 论文笔记
Introduction 这篇文章设计了一种自监督网络框架,能够同时提取特征点的位置以及描述子.相比于patch-based方法,本文提出的算法能够在原始图像提取到像素级精度的特征点的位置及其描述子. ...
- Java安全之BCEL ClassLoader
目录 Java安全之BCEL ClassLoader 写在前面 About BCEL 调试分析 食用姿势 Fuzz反序列化Gadget Fastjson BCEL Payload Thymeleaf ...
- 浅谈VPC (转)
来源于知乎:https://zhuanlan.zhihu.com/p/33658624 VPC全称是Virtual Private Cloud,翻译成中文是虚拟私有云.但是在有些场合也被翻译成私有网络 ...
- Servlet核心内容
Servlet重要的四个生命周期方法 构造方法: 创建servlet对象的时候调用.默认情况下,第一次访问servlet的时候创建servlet对象只调用1次.证明servlet对象在tomcat是单 ...
- SEL类型
1.什么是SEL类型 SEL类型代表着方法的签名,在类对象的方法列表中存储着该签名与方法代码的对应关系 每个类的方法列表都存储在类对象中 每个方法都有一个与之对应的SEL类型的对象 根据一个SEL对象 ...
- 添加了tabBar之后就不能使用wx.navigateTo跳转页面
我在app.json中添加了增加底部选项卡的代码如下: { "pages": [ "pages/index/index", "pages/lo ...
- 鸟哥的Linux私房菜学习笔记——文件权限与目录配置
Linux的文件权限和目录配置 在linux中的每个用户必需属于一个组,不能独立于组外.在linux中每个文件有所有者.所在组.其它组的概念. (1)所有者 一般为文件的创建者,谁创建了该文件,就是天 ...
- 帆软报表(finereport)雷达图钻取详细点新页面展示
添加参数栏,季度下拉框的空间名为combobox0 添加雷达图,通过第三页面做跳转 雷达图钻取.cpt为联动钻取的第三页面 添加纬度(所点击钻取的点) 参数 wd 添加季度参数 jd 值为季 ...
- C语言中各种输入函数之间的区别
以下内容全部来自Bay(百度百科) scanf的返回值 scanf()函数返回成功赋值的数据项数,读到文件末尾出错时则返回EOF. 如: scanf("%d%d", &a, ...