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--------------------------------------- ...
随机推荐
- mysql新增用户无法登陆问题解决ERROR 1045 (28000)
mysql增加新用户无法登陆解决方法 ERROR 1045 (28000): Access denied for user 'appadmin'@'localhost' (using password ...
- 6. java IO 流
一.流的分类: * 1.操作数据单位:字节流.字符流 * 2.数据的流向:输入流.输出流 * 3.流的角色:节点流.处理流 *二.流的体系结构 * 抽象基类 节点流(或文件 ...
- el表达式中的${param}用法
el表达式中的${param}? 1. 2. ${param.name} 等价于 request.getParamter("name"),这两种方法一般用于服务器从页面或者客户端获 ...
- 如何在pyqt中通过OpenCV实现对窗口的透视变换
窗口的透视变换效果 当我们点击UWP应用中的小部件时,会发现小部件会朝着鼠标点击位置凹陷下去,而且不同的点击位置对应着不同的凹陷情况,看起来就好像小部件在屏幕上不只有x轴和y轴,甚至还有一个z轴.要做 ...
- POJ 1927 Area in Triangle 题解
link Description 给出三角形三边长,给出绳长,问绳在三角形内能围成的最大面积.保证绳长 \(\le\) 三角形周长. Solution 首先我们得知道,三角形的内切圆半径就是三角形面积 ...
- 洛谷P7814 「小窝 R3」心の記憶
题意 第一行给定两个数字\(n\) \(m\) \((m \ge n)\)分别代表给定字符串长度以及需要构造出的字符串长度 第二行给定一个长度为\(n\)的字符串 (假设原来的字符串是\(a\) 需要 ...
- bom案例6-轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python日志装饰器实现
问题出自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143184355 ...
- NSSet和NSMutableSet - By吴帮雷
1.NSSet的使用 [NSSet setWithSet:(NSSet *)set]; 用另外一个set对象构造 [NSSet setWithArray:(NSArray *)array];用数组构造 ...
- C++实现对Json数据的友好处理
背景 C/C++客户端需要接收和发送JSON格式的数据到后端以实现通讯和数据交互.C++没有现成的处理JSON格式数据的接口,直接引用第三方库还是避免不了拆解拼接.考虑到此项目将会有大量JSON数据需 ...