URL
链接:https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/
题目
分析
源码
1. 工程结构
├── main_599.c
└── src
├── utarray.h
├── uthash.h
├── utlist.h
├── utringbuffer.h
├── utstack.h
└── utstring.h
2.uthash库下载
github:https://github.com/troydhanson/uthash.git
CSDN文档:https://download.csdn.net/download/yujianliam/84995197
3.main
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include "src/uthash.h"
#define MIN(a,b) ((a)<(b)?(a):(b))
typedef struct {
char *key;
int val;
UT_hash_handle hh;
}HashItem;
void free_hash(HashItem ** obj){
HashItem *curr = NULL, * next = NULL;
HASH_ITER(hh , *obj, curr , next){
HASH_DEL(*obj , curr);
free(curr);
}
}
char ** find_restaurant(char ** list1, int list1_size, char ** list2 , int list2_size, int * return_size){
HashItem * index = NULL;
HashItem * p_entry = NULL;
for (int i = 0; i < list1_size; ++i){
HASH_FIND_STR(index, list1[i], p_entry);
if (NULL == p_entry){
p_entry = (HashItem *)malloc(sizeof(HashItem));
p_entry->key = list1[i];
p_entry->val = i;
HASH_ADD_STR(index, key, p_entry);
}
}
char ** ret = (char **)malloc(sizeof(char *) * MIN(list1_size, list2_size));
int pos = 0;
int index_sum = INT_MAX;
for (int i = 0; i < list2_size; ++i){
HASH_FIND_STR(index, list2[i], p_entry);
if (NULL != p_entry){
int j = p_entry->val;
if (i + j < index_sum){
pos = 0;
ret[pos++] = list2[i];
index_sum = i + j;
}else if (i + j == index_sum){
ret[pos++] = list2[i];
}
}
}
free_hash(&index);
*return_size = pos;
return ret;
}
int main()
{
#if 1
// test data
const char *data1[] = {"Shogun", "Tapioca Express", "Burger King", "KFC"};
const char *data2[] = {"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"};
#else
const char *data1[] = {"Shogun", "Tapioca Express", "Burger King", "KFC"};
const char *data2[] = {"KFC", "Shogun", "Burger King"};
#endif
// define
int list1_size = sizeof(data1)/sizeof(data1[1]);
int list2_size = sizeof(data2)/sizeof(data2[1]);
char ** list1 = (char **)malloc(sizeof(char *) * list1_size);
char ** list2 = (char **)malloc(sizeof(char *) * list2_size);
int ret_size = 0;
// malloc
for (int i = 0; i < list1_size; ++i){
list1[i] = (char *)malloc(sizeof(char) * 50);
}
for (int i = 0; i < list2_size; ++i){
list2[i] = (char *)malloc(sizeof(char) * 50);
}
// init
for (int i = 0; i < list1_size; ++i){
memset(list1[i], '\0', 50);
}
for (int i = 0; i < list2_size; ++i){
memset(list2[i], '\0', 50);
}
// before
printf("=> before \n");
printf("list1 : ");
for (int i = 0; i < list1_size; ++i){
strcpy(list1[i], data1[i]);
printf("%s, ",list1[i]);
}
printf("\nlist2 : ");
for (int i = 0; i < list2_size; ++i){
strcpy(list2[i], data2[i]);
printf("%s, ",list2[i]);
}
// after
printf("\n=> after \n");
char ** ret = find_restaurant(list1, list1_size, list2, list2_size, &ret_size);
for (int i = 0; i < ret_size; ++i){
printf("%s , ",ret[i]);
}
printf("\n");
// free
free(ret);
for (int i = 0; i < list1_size; ++i){
free(list1[i]);
}
free(list1);
for (int i = 0; i < list2_size; ++i){
free(list2[i]);
}
free(list2);
return 0;
}
4. LOG参考
=> before
list1 : Shogun, Tapioca Express, Burger King, KFC,
list2 : Piatti, The Grill at Torrey Pines, Hungry Hunter Steakhouse, Shogun,
=> after
Shogun ,
源码概述
- 使用了一个开源c库UThash,用于hash操作。
- 将list1的记录到hash表中,遍历list2与list1的hash作比较
2.1 这里有一个权重记录,如果原始列表的喜好程度依次递减,这里可以大概表示为最优解。- hash表需要free操作
小结
- 利用HASH做比较
- 使用了开源了C库UThash