# One of the testcases maybe wrong for C language.

• I get a similar situation with the problem 251. Flatten 2D Vector.

https://leetcode.com/discuss/68749/i-suspect-that-the-judge-is-wrong-for-c-language

the testcase begin with

``````[[-35,-73,-20,57,51,-14,102,73,-82,-25,-45,-90,95,68,27,-18,-29,-77,-3,-2,19,24,8,-55,-80,-55,-31,55,-95,-
``````

maybe wrong for C language. it has 105 rows and total 10973 integers.

My c solution:

``````struct NestedIterator {
struct NestedInteger ***stack;
int *ind;
int *size;
int stackSize;
int len;
int cache;
};

struct NestedIterator *nestedIterCreate(struct NestedInteger** nestedList, int nestedListSize) {
struct NestedIterator *iter = malloc(sizeof(struct NestedIterator));
iter->stackSize = 1000;
iter->stack = malloc(iter->stackSize * sizeof(struct NestedInteger **));
iter->ind = malloc(iter->stackSize * sizeof(int));
iter->size = malloc(iter->stackSize * sizeof(int));
iter->len = 1;
iter->stack[0] = nestedList;
iter->ind[0] = 0;
iter->size[0] = nestedListSize;
return iter;
}

bool nestedIterHasNext(struct NestedIterator *iter) {
if (!iter->len) return false;
if (iter->ind[iter->len - 1] == iter->size[iter->len - 1]) {
iter->len--;
return nestedIterHasNext(iter);
}
struct NestedInteger **tgt = iter->stack[iter->len - 1][iter->ind[iter->len - 1]];
if (NestedIntegerIsInteger(tgt)) return true;
iter->ind[iter->len - 1]++;
if (iter->len == iter->stackSize) {
iter->stackSize += 1000;
iter->stack = realloc(iter->stack, iter->stackSize * sizeof(struct NestedInteger **));
iter->ind = realloc(iter->ind, iter->stackSize * sizeof(int));
iter->size = realloc(iter->size, iter->stackSize * sizeof(int));
}
iter->stack[iter->len] = NestedIntegerGetList(tgt);;
iter->ind[iter->len] = 0;
iter->size[iter->len] = NestedIntegerGetListSize(tgt);
iter->len++;
return nestedIterHasNext(iter);
}

int nestedIterNext(struct NestedIterator *iter) {
struct NestedInteger **tgt = iter->stack[iter->len - 1][iter->ind[iter->len - 1]];
iter->ind[iter->len - 1]++;
return NestedIntegerGetInteger(tgt);
}

/** Deallocates memory previously allocated for the iterator */
void nestedIterFree(struct NestedIterator *iter) {
free(iter->stack);
free(iter->ind);
free(iter->size);
free(iter);
}

/**
* Your NestedIterator will be called like this:
* struct NestedIterator *i = nestedIterCreate(nestedList, nestedListSize);
* while (nestedIterHasNext(i)) printf("%d\n", nestedIterNext(i));
* nestedIterFree(i);
*/``````

• I got stuck in the same situation. But if I solve it with C++, it's fine. Do you find the reason?

``````struct NestedIterator {
int* flattenedList;
long long listSize;
long long iteratorPos;
};
void flattenNestedList(struct NestedInteger** nestedList, int nestedListSize, struct NestedIterator* iter) {
int i;
struct NestedInteger** curNestedList;
int size;
for(i = 0; i < nestedListSize; i++) {
if(NestedIntegerIsInteger(nestedList[i])) {
iter->flattenedList[(iter->listSize)++] = NestedIntegerGetInteger(nestedList[i]);
} else {
curNestedList = NestedIntegerGetList(nestedList[i]);
size = NestedIntegerGetListSize(nestedList[i]);
flattenNestedList(curNestedList, size, iter);
}
}
}
struct NestedIterator *nestedIterCreate(struct NestedInteger** nestedList, int nestedListSize) {
struct NestedIterator* iter = malloc(sizeof(struct NestedIterator));
iter->flattenedList = calloc(100000, sizeof(int));
iter->listSize = 0;
iter->iteratorPos = 0;

flattenNestedList(nestedList, nestedListSize, iter);
return iter;
}

bool nestedIterHasNext(struct NestedIterator *iter) {
return iter->iteratorPos < iter->listSize;
}

int nestedIterNext(struct NestedIterator *iter) {
return iter->flattenedList[(iter->iteratorPos)++];
}

/** Deallocates memory previously allocated for the iterator */
void nestedIterFree(struct NestedIterator *iter) {
iter->listSize = 0;
iter->iteratorPos = 0;
free(iter->flattenedList);
free(iter);
}
``````

• @1337c0d3r
Hi Admin, can you check this, please? I am stuck on the same test case using C language.

• This post is deleted!

• This post is deleted!

• I am also getting the same test case failure :( Would love to see this issue resolved.

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