2025年10月6日 星期一

[341] Flatten Nested List Iterator

怎麼沒有人要用Linked list !!! 害我顯的很慢!!!可惡XD!!!

其實題目有一點看不太懂~題意不太清楚
nest list 最終都會剩下只有 "an int" 的這件事頗搞混
應該是說只剩下int list , 而不會再有nest list 了(吧) 
總之

/**
* *********************************************************************
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* *********************************************************************
*
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* bool NestedIntegerIsInteger(struct NestedInteger *);
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* int NestedIntegerGetInteger(struct NestedInteger *);
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* struct NestedInteger **NestedIntegerGetList(struct NestedInteger *);
*
* // Return the nested list's size that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* int NestedIntegerGetListSize(struct NestedInteger *);
* };
*/
struct NestedIterator {
struct ListNode *head;
struct ListNode *curr;
struct ListNode *tail;
};

void createNode(struct NestedIterator *iter, int val) {
if (iter->head == NULL)
{
iter->head = calloc (1, sizeof(struct ListNode));
iter->head->val = val;
iter->tail = iter->head;
}
else
{
struct ListNode *ptr = calloc (1, sizeof(struct ListNode));
ptr->val = val;
iter->tail->next = ptr;
iter->tail = ptr;
}
}

void nestedRecursive(struct NestedIterator *ptr,struct NestedInteger** nestedList, int nestedListSize) {

for (int i=0; i<nestedListSize; i++)
{
if (NestedIntegerIsInteger(nestedList[i]))
createNode(ptr, NestedIntegerGetInteger(nestedList[i]));
else
nestedRecursive(ptr,NestedIntegerGetList(nestedList[i]),
NestedIntegerGetListSize(nestedList[i]));
}
return;
}

struct NestedIterator *nestedIterCreate(struct NestedInteger** nestedList, int nestedListSize) {
struct NestedIterator *iter = calloc (1, sizeof(struct NestedIterator));
iter->head = NULL;
iter->tail = NULL;
nestedRecursive(iter,nestedList,nestedListSize);
iter->curr = iter->head;
return iter;
}

bool nestedIterHasNext(struct NestedIterator *iter) {
return (iter->curr == NULL)?(false):(true);
}

int nestedIterNext(struct NestedIterator *iter) {
int val = iter->curr->val;
iter->curr = iter->curr->next;
return val;
}

/** Deallocates memory previously allocated for the iterator */
void nestedIterFree(struct NestedIterator *iter) {
struct ListNode *ptr = iter->head;
struct ListNode *pfree;
while (ptr != NULL)
{
pfree = ptr;
ptr = ptr->next;
pfree->next = NULL;
free(pfree);
}
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);
*/

沒有留言:

張貼留言