leetcode字符串|-【LeetCode】字母异位词分组 - free(temp->
【LeetCode】字母异位词分组
The Begin点点关注,收藏不迷路
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
示例 1:
输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
示例 2:
输入: strs = [“”]
输出: [[“”]]
示例 3:
输入: strs = [“a”]
输出: [[“a”]]
提示:
1 <= strs.length <= 10^4
0 <= strs[i].length <= 100
strs[i] 仅包含小写字母
#include
#include
#include
typedef struct Node {
char* key;
int* indices;
int size;
int capacity;
struct Node* next;
} Node;
typedef struct HashTable {
Node** buckets;
int size;
int capacity;
} HashTable;
// 对字符串进行排序,辅助函数
void sortString(char* str) {
int len = strlen(str);
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (str[j] > str[j + 1]) {
char temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
// 创建新的节点
Node* createNode(char* key, int index) {
Node* node = (Node*)malloc(sizeof(Node));
node->key = strdup(key);
node->size = 1;
node->capacity = 2;
node->indices = (int*)malloc(node->capacity * sizeof(int));
node->indices[0] = index;
node->next = NULL;
return node; 拼多多帮助力入口
}
// 创建哈希表
HashTable* createHashTable(int capacity) {
HashTable* table = (HashTable*)malloc(sizeof(HashTable));
table->buckets = (Node**)malloc(capacity * sizeof(Node*));
for (int i = 0; i < capacity; i++) {
table->buckets[i] = NULL;
}
table->size = 0;
table->capacity = capacity;
return table;
}
// 哈希函数
int hashFunction(char* key, int capacity) {
int hash = 0;
int len = strlen(key);
for (int i = 0; i < len; i++) {
hash = (hash * 31 + key[i]) % capacity;
}
return hash;
}
// 在哈希表中查找节点
Node* findNode(HashTable* table, char* key) {
int index = hashFunction(key, table->capacity);
Node* curr = table->buckets[index];
while (curr) {
if (strcmp(curr->key, key) == 0) {
return curr;
}
curr = curr->next;
}
return NULL;
}
// 插入节点到哈希表
void insertNode(HashTable* table, char* key, int index) {
int indexInTable = hashFunction(key, table->capacity);
Node* node = findNode(table, key);
if (node == NULL) {
Node* newNode = createNode(key, index);
newNode->next = table->buckets[indexInTable];
table->buckets[indexInTable] = newNode;
table->size++;
} else {
if (node->size == node->capacity) {
node->capacity *= 2;
node->indices = (int*)realloc(node->indices, node->capacity * sizeof(int));
}
node->indices[node->size] = index;
node->size++;
}
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** returnColumnSizes) {
HashTable* table = createHashTable(strsSize);
for (int i = 0; i < strsSize; i++) {
char sorted[101];
strcpy(sorted, strs[i]);
sortString(sorted);
insertNode(table, sorted, i);
}
*returnSize = table->size;
*returnColumnSizes = (int*)malloc((*returnSize) * sizeof(int));
char*** result = (char***)malloc((*returnSize) * sizeof(char**));
int index = 0;
for (int i = 0; i < table->capacity; i++) {
Node* curr = table->buckets[i];
while (curr) {
(*returnColumnSizes)[index] = curr->size;
result[index] = (char**)malloc(curr->size * sizeof(char*));
for (int j = 0; j < curr->size; j++) {
result[index][j] = strdup(strs[curr->indices[j]]);
}
index++;
curr = curr->next;
}
}
// 释放哈希表内存
for (int i = 0; i < table->capacity; i++) {
Node* curr = table->buckets[i];
while (curr) {
Node* temp = curr;
curr = curr->next;
free(temp->key);
free(temp->indices);
free(temp);
}
}
free(table->buckets);
free(table);
return result;
}
The End点点关注,收藏不迷路