参考: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使用的更多相关文章

  1. C开源hash项目uthash

    uthash 是C的比较优秀的开源代码,它实现了常见的hash操作函数,例如查找.插入.删除等.该套开源代码采用宏的方式实现hash函数的相关功能,支持C语言的任意数据结构最为key值,甚至可以采用多 ...

  2. C 语言资源大全中文版

    C 语言资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-c 是 koz.ross 发起维护的 C 语言资源列表,内容包括了: ...

  3. C :uthash

    参考: [1] uthash | 学步园 [2] 源码 [3] 官方文档 [4] [5] 一.哈希表的概念及作用 在一般的线性表或者树中,我们所储存的值写它的存储位置的关系是随机的.因此,在查找过程中 ...

  4. C语言 · 高精度加法

    问题描述 输入两个整数a和b,输出这两个整数的和.a和b都不超过100位. 算法描述 由于a和b都比较大,所以不能直接使用语言中的标准数据类型来存储.对于这种问题,一般使用数组来处理. 定义一个数组A ...

  5. Windows server 2012 添加中文语言包(英文转为中文)(离线)

    Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...

  6. iOS开发系列--Swift语言

    概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...

  7. C语言 · Anagrams问题

    问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...

  8. C语言 · 字符转对比

    问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...

  9. JAVA语言中的修饰符

    JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...

随机推荐

  1. 中文分词,自然语言处理器NLP。 新版本已上线,增加二级行业分类。

    一  cacl2新版本上线,在一级行业的基础上深度挖掘,新增了对应的二级分类. 可以查看一级行业[农林牧渔],下面对应的所有二级行业的词库.这里拿[林业]来观察效果. 具体分词的效果. 二    Gi ...

  2. AWS SAA_C01 考试分享。

    Saa-c01 经验分享! 序言1.介绍自己的情况,我是一个做后台开发的初级java程序员.还是处于在写业务逻辑的阶段,我对aws可谓是啥都不懂,纯种的小白,完全是从0基础开始学习的.希望分享一些我的 ...

  3. tmux 入门教程

    tmux 本教程是基于ACWing的<Linux基础课>所做,希望大家支持ACWing 功能 分屏 当需要同时运行两个终端,并且进行比对着输入时,来回切换比较麻烦,就可以利用分屏 可以在一 ...

  4. Halcon视觉入门芯片识别

    Halcon视觉入门芯片识别 需求 有如下图的一个摆盘,摆盘的方格中摆放芯片,一个格子中只放一个,我们需要知道每个方格中是否有芯片去指导我们将芯片放到空的方格中. 分析 通过图片分析得出 我们感兴趣的 ...

  5. Springboot+Redisson自定义注解一次解决重复提交问题(含源码)

    前言   项目中经常会出现重复提交的问题,而接口幂等性也一直以来是做任何项目都要关注的疑难点,网上可以查到非常多的方案,我归纳了几点如下:   1).数据库层面,对责任字段设置唯一索引,这是最直接有效 ...

  6. fiddler模拟弱网1

    第一步: 首先你得将你的fiddle配置好了,并链接上了移动端. 参考 这篇文章:http://www.cnblogs.com/lijiageng/p/6214162.html     第二步: 使用 ...

  7. 用Express 创建项目

    1.Node.js Express 框架安装:npm install express --save在当前目录下创建一个node_modules 2.安装必要的中间件npm install body-p ...

  8. 02网络编程( socket套接字+TCP粘包 )

    目录 02 网络编程 一.socket套接字编程 二.简易代码模板 2.1 服务端 2.2 客户端 三.通信循环及代码优化 四.黏包现象 五.struct模块 六.简易版本报头 七.上传文件数据 * ...

  9. Netty核心原理

    Netty核心原理 1. Netty介绍 1.1 原生NIO存在的问题 NIO的类库和API使用繁杂 需要具备其他额外的技能,如java多线程编程等才能编写出高质量的NIO程序 开发工作量和难度都非常 ...

  10. c++ 聚合/POD/平凡/标准布局 介绍

    目录 前言 聚合 POD(Plain Old Data) 平凡类型(TrivialType) 要求 平凡可复制(TrivialCopyable) 要求 对于某些函数的补充说明 平凡拷贝构造函数 符合条 ...