C solution, don't know why it will fail when rowIndex = 6 or 10


  • 0
    B

    My C solution. It will pass all test cases(i.e. 100+ lines). Even though it is reporting rowIndex = 6 or 10 and etc will have runtime error. But I tested in GCC environment, it works. Don't know why.

    The error message is "sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed."

    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* getRow(int rowIndex, int* returnSize) {
        int row = 1, column;
        int* preRowNum = (int*)malloc(rowIndex * sizeof(int));
        int* rowNum = (int*)malloc(rowIndex * sizeof(int));
        
        if(rowIndex < 0) return NULL;
        
        preRowNum[0] = 1;
        rowNum[0] = 1;
        
        while(row <= rowIndex)
        {
            for(column = 0; column < (row + 1); column++)
            {
                if((column == 0) || (column == row)) rowNum[column] = 1;
                else rowNum[column] = preRowNum[column-1] + preRowNum[column];
            }
            
            memcpy(preRowNum, rowNum, ((row+1) * sizeof(int)));
            
            row++;
        }
        
        *returnSize = rowIndex + 1;
        
        return rowNum;
    }
    

Log in to reply
 

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