"Submit solution" returns different result with "Run code" (C solution using uthash table)


  • 0
    C

    When I submit my code, it returns "Wrong Answer" with the ["",""] test case.

    But when I copied ["",""] test case and click "Run code", I got the same result of "Your answer" and "Expected answer".

    What the magic?

    0_1469362000949_Screenshot from 2016-07-24 20-02-46.png

    0_1469361954887_Screenshot from 2016-07-24 20-03-19.png

    Following is my code:

    typedef
    struct hb {
        void *key;            /* we'll use this field as the key */
        char **value;
        int  n;
        UT_hash_handle hh; /* makes this structure hashable */
    } hb_t;
    
    void mergeSort(char *str, char *tmp, int left, int right)
    {
        int mid;
        int i, j, k;
    
        if (left >= right) return;
    
        mid = (left + right) / 2;
        mergeSort(str, tmp, left, mid);
        mergeSort(str, tmp, mid + 1, right);
    
        i = left;
        j = mid + 1;
        k = i;
    
        while (i <= mid && j <= right) {
            if (str[i]  < str[j])
                tmp[k++] = str[i++];
            else tmp[k++] = str[j++];
        }
    
        while (i <= mid) tmp[k++] = str[i++];
        while (j <= right) tmp[k++] = str[j++];
    
        /* copy sorted sequence back */
        for (k = left ; k <= right ; ++k)
            str[k] = tmp[k];
    }
    
    char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int* returnSize) {
        int     i, l;
        char    *tmp, *tmp_s;
        hb_t    *users;
        hb_t    *t;
        char    ***matrix;
    
        tmp = NULL, users = NULL;
        for (i = 0 ; i < strsSize ; ++i) {
            l = strlen(strs[i]);
            tmp = calloc(l, sizeof(char));
            tmp_s = malloc(l * sizeof(char));
            memcpy(tmp_s, strs[i], l);
            mergeSort(tmp_s, tmp, 0, l - 1);
    
            HASH_FIND_STR(users, tmp_s, t);
            if (!t) {
                t = malloc(sizeof *t);
                t->key = tmp;
                t->n = 1;
                t->value = (char **)malloc(sizeof(char *));
                t->value[0] = strs[i];
                HASH_ADD_KEYPTR(hh, users, t->key, strlen(t->key), t);
            } else {
                ++t->n;
                t->value = realloc(t->value, t->n * sizeof(char *));
                t->value[t->n - 1] = strs[i];
            }
            free(tmp);
        }
    
        *returnSize = HASH_COUNT(users);
        *columnSizes = (int *)calloc(*returnSize, sizeof(int));
        matrix = malloc(*returnSize * sizeof(char **));
    
        for (t = users, i = 0 ; t ; t = t->hh.next, ++i) {
            (*columnSizes)[i] = t->n;
            matrix[i] = t->value;
        }
    
        return matrix;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.