forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add go solution to lc problem: No.0341 (doocs#855)
No.0341.Flatten Nested List Iterator
- Loading branch information
Showing
3 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
solution/0300-0399/0341.Flatten Nested List Iterator/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* // This is the interface that allows for creating nested lists. | ||
* // You should not implement it, or speculate about its implementation | ||
* type NestedInteger struct { | ||
* } | ||
* | ||
* // Return true if this NestedInteger holds a single integer, rather than a nested list. | ||
* func (this NestedInteger) IsInteger() bool {} | ||
* | ||
* // 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 | ||
* // So before calling this method, you should have a check | ||
* func (this NestedInteger) GetInteger() int {} | ||
* | ||
* // Set this NestedInteger to hold a single integer. | ||
* func (n *NestedInteger) SetInteger(value int) {} | ||
* | ||
* // Set this NestedInteger to hold a nested list and adds a nested integer to it. | ||
* func (this *NestedInteger) Add(elem NestedInteger) {} | ||
* | ||
* // Return the nested list that this NestedInteger holds, if it holds a nested list | ||
* // The list length is zero if this NestedInteger holds a single integer | ||
* // You can access NestedInteger's List element directly if you want to modify it | ||
* func (this NestedInteger) GetList() []*NestedInteger {} | ||
*/ | ||
|
||
type NestedIterator struct { | ||
nested *list.List | ||
} | ||
|
||
func Constructor(nestedList []*NestedInteger) *NestedIterator { | ||
nested := list.New() | ||
for _, v := range nestedList { | ||
nested.PushBack(v) | ||
} | ||
return &NestedIterator{nested: nested} | ||
} | ||
|
||
func (this *NestedIterator) Next() int { | ||
res := this.nested.Front().Value.(*NestedInteger) | ||
this.nested.Remove(this.nested.Front()) | ||
return res.GetInteger() | ||
} | ||
|
||
func (this *NestedIterator) HasNext() bool { | ||
for this.nested.Len() > 0 && !this.nested.Front().Value.(*NestedInteger).IsInteger() { | ||
front := this.nested.Front().Value.(*NestedInteger) | ||
this.nested.Remove(this.nested.Front()) | ||
nodes := front.GetList() | ||
for i := len(nodes) - 1; i >= 0; i-- { | ||
this.nested.PushFront(nodes[i]) | ||
} | ||
} | ||
return this.nested.Len() > 0 | ||
} |