字母异位词c语言,leetcode-49-字母异位词分组-C语言

/**

* Return an array of arrays of size *returnSize.

* The sizes of the arrays are returned as *columnSizes array.

* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().

*/

typedef struct node{

int index;

int key;

int cnt[26];

} Node;

#define LEN 1000

typedef struct hash_node_t{

int index;

int ret_index;

int col_index;

Node *wnode;

struct hash_node_t *next;

} HaNode;

int hash_index(Node *node){

return node->key % LEN;

}

void hash_init(HaNode **hash_table, int len){

int i;

for(i=0; inext;

free(last);

}

}

return;

}

void hash_insert(HaNode **hash_table, HaNode *node){

int index = hash_index(node->wnode);

node->next = hash_table[index];

hash_table[index] = node;

}

HaNode *hash_find(HaNode **hash_table, Node *wnode, int key){

int index = hash_index(wnode);

HaNode *p = hash_table[index];

while(p){

if(p->wnode->key == key && !memcmp(wnode->cnt, p->wnode->cnt, 26*sizeof(int))){

break;

}

p = p->next;

}

return p;

}

/*

* 方法二:

* 时间复杂度O(N),每访问一个单词时,到hash表中查找,看是否有能匹配的单词,如果有的话,直接使用找到的hash节点信息,

* 将当前单词插入到返回结果中,并修改hash节点信息。

* 如果查找不到,则需要重新构建哈希节点,并插入哈希表。

*/

char*** groupAnagrams(char** strs, int strsSize, int* returnSize, int** columnSizes) {

int i, j;

Node *arr = (Node *)malloc(sizeof(Node) * strsSize);

bool *vist = (bool *)malloc(sizeof(bool) * strsSize);

char *** ret = (char ***)malloc(sizeof(char**) * strsSize);

int *col_size = (int *)malloc(sizeof(int) * strsSize);

int ret_index = 0;

int col_index = 0;

HaNode **hash_table = (HaNode **)malloc(sizeof(HaNode *) * LEN);

HaNode *p = NULL;

int key;

hash_init(hash_table, LEN);

memset(arr, 0, sizeof(Node) * strsSize);

for(i=0; iret_index][p->col_index++] = strs[i];

col_size[p->ret_index] = p->col_index;

continue;

}

/* 在hash表中没找到,生成新的哈希节点,并插入哈希表 */

p = (HaNode *)malloc(sizeof(HaNode));

p->ret_index = ret_index;

p->col_index = 0;

p->wnode = &arr[i];

ret[p->ret_index] = (char **)malloc(sizeof(char *) * strsSize);

ret[p->ret_index][p->col_index++] = strs[i];

col_size[p->ret_index] = p->col_index;

hash_insert(hash_table, p);

ret_index++;

}

free(vist);

free(arr);

hash_exit(hash_table, LEN);

free(hash_table);

*columnSizes = col_size;

*returnSize = ret_index;

return ret;

}

/* 方法一:

* 时间复杂度O(N*N),遍历全部残次,当遍历到当前位置单词时,使用该位置后的所有单词和当前位置单词相比较;

* 相同则插入旧的位置,否则构建新的位置;

* 注意这里需要使用key进行优化,每次两个单词进行比较,首先比较key;

* 每次比较不同的时候,不用比较整个hash字母表了,否则会超时

*/

char*** groupAnagrams_bak(char** strs, int strsSize, int* returnSize, int** columnSizes) {

int i, j;

Node *arr = (Node *)malloc(sizeof(Node) * strsSize);

bool *vist = (bool *)malloc(sizeof(bool) * strsSize);

char *** ret = (char ***)malloc(sizeof(char**) * strsSize);

int *col_size = (int *)malloc(sizeof(int) * strsSize);

int ret_index = 0;

int col_index = 0;

HaNode **hash_table = (HaNode **)malloc(sizeof(HaNode *) * LEN);

HaNode *p = NULL;

int key;

hash_init(hash_table, LEN);

memset(arr, 0, sizeof(Node) * strsSize);

for(i=0; i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值