leetcode刷题,uthash总是有问题,head指的东西为什么会变呢?

#define POOL_SIZE 20000
#define debug printf

typedef struct hasnode 
{
    char *key;
    int cnt;
    UT_hash_handle hh;
} hashnode_t;

int g_flag_letter[26] = {0};
int g_index = 0;
int type_cnt = 0;


int maxFreq(char * s, int maxLetters, int minSize, int maxSize){
    hashnode_t *pool = (hashnode_t *)calloc(POOL_SIZE, sizeof(hashnode_t));
    hashnode_t *head = NULL;
    int slen = strlen(s);
    char tempstr[27] = {0};
    int max_cnt = 0;
    int psize = 0;
    hashnode_t *tmp1 = NULL;
    hashnode_t *temp2 = NULL;

    memset(g_flag_letter, 0, 26);
    g_index = 0;
    type_cnt = 0;

    while(s[g_index + minSize - 1] != '\0') {
        memcpy(tempstr, (s + g_index), minSize);
        tempstr[minSize] = '\0';

        if (judge_str(tempstr, maxLetters)) {
            hashnode_t *cur = &pool[psize];
            cur->cnt = 1;
            cur->key = tempstr;

            hashnode_t *tmph;
            HASH_FIND(hh, head, cur->key, minSize, tmph);
            if (tmph == NULL) {
                HASH_ADD_KEYPTR(hh, head, cur->key, minSize, cur);
                psize++;
                max_cnt = max_cnt > 1 ? max_cnt : 1;
                debug("[%d]headletter:%s\n", __LINE__, head->key); //这里为什么head会变?
            } else {
                (tmph->cnt)++;
                max_cnt = max_cnt > tmph->cnt ? max_cnt : tmph->cnt;
            }
        }

        g_flag_letter[*tempstr - 'a']--;
        if (g_flag_letter[*tempstr - 'a'] == 0) {
            type_cnt--;
        }
        g_index++;
    }

    int headcount = HASH_COUNT(head);
    hashnode_t *ptemp;
    for(ptemp=head; ptemp != NULL; ptemp=ptemp->hh.next) {  
        printf("user ikey %s: value %d\n", ptemp->key, ptemp->cnt);  
    }

    free(pool);

    return max_cnt;
}

打印结果:
[76]headletter:ccbb
[76]headletter:cbbe
[76]headletter:bbee
[76]headletter:beed
[76]headletter:edea
[76]headletter:deaa
[76]headletter:eaab
[76]headletter:aabe
[76]headletter:beec
[76]headletter:eecc
[92]headletter:1500698912
user ikey eecc: value 1
user ikey eecc: value 2
user ikey eecc: value 1
user ikey eecc: value 1
user ikey eecc: value 1
user ikey eecc: value 2
user ikey eecc: value 1
user ikey eecc: value 1
user ikey eecc: value 1
user ikey eecc: value 1

入参:
"ccbbeedeaabeecc"
4
4
4

没弄明白哪里搞错了

查看全部
lovemust
_soloangel
2020/03/30 00:05
  • c++
  • c语言
  • bash
  • 开发语言
  • 点赞
  • 收藏
  • 回答
    私信

1个回复