Skip to content

Commit 6e9dcd4

Browse files
authored
feat: add go solution to lc problem: No.0341 (doocs#855)
No.0341.Flatten Nested List Iterator
1 parent 998cecd commit 6e9dcd4

File tree

3 files changed

+309
-0
lines changed

3 files changed

+309
-0
lines changed

solution/0300-0399/0341.Flatten Nested List Iterator/README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ return res</pre>
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:递归**
61+
62+
根据题意要求可以将 NestedInteger 数据结构视作一个 N 叉树,当为一个整数时该节点是 N 叉树的叶子节点,当为一个整数数组时该节点是 N 叉树的非叶子节点,数组中的每一个元素包含子树的所有节点。故直接递归遍历 N 叉树并记录所有的叶子节点即可。
63+
64+
**方法二:直接展开**
65+
66+
调用 hasNext 时,如果 nestedList 的第一个元素是列表类型,则不断展开这个元素,直到第一个元素是整数类型。 调用 Next 方法时,由 hasNext 方法已确保 nestedList 第一个元素为整数类型,直接返回即可。
67+
6068
<!-- tabs:start -->
6169

6270
### **Python3**
@@ -312,6 +320,129 @@ impl NestedIterator {
312320
*/
313321
```
314322

323+
### **Go**
324+
325+
递归:
326+
327+
```go
328+
/**
329+
* // This is the interface that allows for creating nested lists.
330+
* // You should not implement it, or speculate about its implementation
331+
* type NestedInteger struct {
332+
* }
333+
*
334+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
335+
* func (this NestedInteger) IsInteger() bool {}
336+
*
337+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
338+
* // The result is undefined if this NestedInteger holds a nested list
339+
* // So before calling this method, you should have a check
340+
* func (this NestedInteger) GetInteger() int {}
341+
*
342+
* // Set this NestedInteger to hold a single integer.
343+
* func (n *NestedInteger) SetInteger(value int) {}
344+
*
345+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
346+
* func (this *NestedInteger) Add(elem NestedInteger) {}
347+
*
348+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
349+
* // The list length is zero if this NestedInteger holds a single integer
350+
* // You can access NestedInteger's List element directly if you want to modify it
351+
* func (this NestedInteger) GetList() []*NestedInteger {}
352+
*/
353+
354+
type NestedIterator struct {
355+
iterator []int
356+
index, length int
357+
}
358+
359+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
360+
result := make([]int, 0)
361+
var traversal func(nodes []*NestedInteger)
362+
traversal = func(nodes []*NestedInteger) {
363+
for _, child := range nodes {
364+
if child.IsInteger() {
365+
result = append(result, child.GetInteger())
366+
} else {
367+
traversal(child.GetList())
368+
}
369+
}
370+
}
371+
traversal(nestedList)
372+
return &NestedIterator{iterator: result, index: 0, length: len(result)}
373+
}
374+
375+
func (this *NestedIterator) Next() int {
376+
res := this.iterator[this.index]
377+
this.index++
378+
return res
379+
}
380+
381+
func (this *NestedIterator) HasNext() bool {
382+
return this.index < this.length
383+
}
384+
```
385+
386+
直接展开:
387+
388+
```go
389+
/**
390+
* // This is the interface that allows for creating nested lists.
391+
* // You should not implement it, or speculate about its implementation
392+
* type NestedInteger struct {
393+
* }
394+
*
395+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
396+
* func (this NestedInteger) IsInteger() bool {}
397+
*
398+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
399+
* // The result is undefined if this NestedInteger holds a nested list
400+
* // So before calling this method, you should have a check
401+
* func (this NestedInteger) GetInteger() int {}
402+
*
403+
* // Set this NestedInteger to hold a single integer.
404+
* func (n *NestedInteger) SetInteger(value int) {}
405+
*
406+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
407+
* func (this *NestedInteger) Add(elem NestedInteger) {}
408+
*
409+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
410+
* // The list length is zero if this NestedInteger holds a single integer
411+
* // You can access NestedInteger's List element directly if you want to modify it
412+
* func (this NestedInteger) GetList() []*NestedInteger {}
413+
*/
414+
415+
type NestedIterator struct {
416+
nested *list.List
417+
}
418+
419+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
420+
nested := list.New()
421+
for _, v := range nestedList {
422+
nested.PushBack(v)
423+
}
424+
return &NestedIterator{nested: nested}
425+
}
426+
427+
func (this *NestedIterator) Next() int {
428+
res := this.nested.Front().Value.(*NestedInteger)
429+
this.nested.Remove(this.nested.Front())
430+
return res.GetInteger()
431+
}
432+
433+
func (this *NestedIterator) HasNext() bool {
434+
for this.nested.Len() > 0 && !this.nested.Front().Value.(*NestedInteger).IsInteger() {
435+
front := this.nested.Front().Value.(*NestedInteger)
436+
this.nested.Remove(this.nested.Front())
437+
nodes := front.GetList()
438+
for i := len(nodes) - 1; i >= 0; i-- {
439+
this.nested.PushFront(nodes[i])
440+
}
441+
}
442+
return this.nested.Len() > 0
443+
}
444+
```
445+
315446
### **...**
316447

317448
```

solution/0300-0399/0341.Flatten Nested List Iterator/README_EN.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,129 @@ impl NestedIterator {
304304
*/
305305
```
306306

307+
### **Go**
308+
309+
recursion:
310+
311+
```go
312+
/**
313+
* // This is the interface that allows for creating nested lists.
314+
* // You should not implement it, or speculate about its implementation
315+
* type NestedInteger struct {
316+
* }
317+
*
318+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
319+
* func (this NestedInteger) IsInteger() bool {}
320+
*
321+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
322+
* // The result is undefined if this NestedInteger holds a nested list
323+
* // So before calling this method, you should have a check
324+
* func (this NestedInteger) GetInteger() int {}
325+
*
326+
* // Set this NestedInteger to hold a single integer.
327+
* func (n *NestedInteger) SetInteger(value int) {}
328+
*
329+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
330+
* func (this *NestedInteger) Add(elem NestedInteger) {}
331+
*
332+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
333+
* // The list length is zero if this NestedInteger holds a single integer
334+
* // You can access NestedInteger's List element directly if you want to modify it
335+
* func (this NestedInteger) GetList() []*NestedInteger {}
336+
*/
337+
338+
type NestedIterator struct {
339+
iterator []int
340+
index, length int
341+
}
342+
343+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
344+
result := make([]int, 0)
345+
var traversal func(nodes []*NestedInteger)
346+
traversal = func(nodes []*NestedInteger) {
347+
for _, child := range nodes {
348+
if child.IsInteger() {
349+
result = append(result, child.GetInteger())
350+
} else {
351+
traversal(child.GetList())
352+
}
353+
}
354+
}
355+
traversal(nestedList)
356+
return &NestedIterator{iterator: result, index: 0, length: len(result)}
357+
}
358+
359+
func (this *NestedIterator) Next() int {
360+
res := this.iterator[this.index]
361+
this.index++
362+
return res
363+
}
364+
365+
func (this *NestedIterator) HasNext() bool {
366+
return this.index < this.length
367+
}
368+
```
369+
370+
Expand directly:
371+
372+
```go
373+
/**
374+
* // This is the interface that allows for creating nested lists.
375+
* // You should not implement it, or speculate about its implementation
376+
* type NestedInteger struct {
377+
* }
378+
*
379+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
380+
* func (this NestedInteger) IsInteger() bool {}
381+
*
382+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
383+
* // The result is undefined if this NestedInteger holds a nested list
384+
* // So before calling this method, you should have a check
385+
* func (this NestedInteger) GetInteger() int {}
386+
*
387+
* // Set this NestedInteger to hold a single integer.
388+
* func (n *NestedInteger) SetInteger(value int) {}
389+
*
390+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
391+
* func (this *NestedInteger) Add(elem NestedInteger) {}
392+
*
393+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
394+
* // The list length is zero if this NestedInteger holds a single integer
395+
* // You can access NestedInteger's List element directly if you want to modify it
396+
* func (this NestedInteger) GetList() []*NestedInteger {}
397+
*/
398+
399+
type NestedIterator struct {
400+
nested *list.List
401+
}
402+
403+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
404+
nested := list.New()
405+
for _, v := range nestedList {
406+
nested.PushBack(v)
407+
}
408+
return &NestedIterator{nested: nested}
409+
}
410+
411+
func (this *NestedIterator) Next() int {
412+
res := this.nested.Front().Value.(*NestedInteger)
413+
this.nested.Remove(this.nested.Front())
414+
return res.GetInteger()
415+
}
416+
417+
func (this *NestedIterator) HasNext() bool {
418+
for this.nested.Len() > 0 && !this.nested.Front().Value.(*NestedInteger).IsInteger() {
419+
front := this.nested.Front().Value.(*NestedInteger)
420+
this.nested.Remove(this.nested.Front())
421+
nodes := front.GetList()
422+
for i := len(nodes) - 1; i >= 0; i-- {
423+
this.nested.PushFront(nodes[i])
424+
}
425+
}
426+
return this.nested.Len() > 0
427+
}
428+
```
429+
307430
### **...**
308431

309432
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* type NestedInteger struct {
5+
* }
6+
*
7+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
8+
* func (this NestedInteger) IsInteger() bool {}
9+
*
10+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
11+
* // The result is undefined if this NestedInteger holds a nested list
12+
* // So before calling this method, you should have a check
13+
* func (this NestedInteger) GetInteger() int {}
14+
*
15+
* // Set this NestedInteger to hold a single integer.
16+
* func (n *NestedInteger) SetInteger(value int) {}
17+
*
18+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
19+
* func (this *NestedInteger) Add(elem NestedInteger) {}
20+
*
21+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
22+
* // The list length is zero if this NestedInteger holds a single integer
23+
* // You can access NestedInteger's List element directly if you want to modify it
24+
* func (this NestedInteger) GetList() []*NestedInteger {}
25+
*/
26+
27+
type NestedIterator struct {
28+
nested *list.List
29+
}
30+
31+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
32+
nested := list.New()
33+
for _, v := range nestedList {
34+
nested.PushBack(v)
35+
}
36+
return &NestedIterator{nested: nested}
37+
}
38+
39+
func (this *NestedIterator) Next() int {
40+
res := this.nested.Front().Value.(*NestedInteger)
41+
this.nested.Remove(this.nested.Front())
42+
return res.GetInteger()
43+
}
44+
45+
func (this *NestedIterator) HasNext() bool {
46+
for this.nested.Len() > 0 && !this.nested.Front().Value.(*NestedInteger).IsInteger() {
47+
front := this.nested.Front().Value.(*NestedInteger)
48+
this.nested.Remove(this.nested.Front())
49+
nodes := front.GetList()
50+
for i := len(nodes) - 1; i >= 0; i-- {
51+
this.nested.PushFront(nodes[i])
52+
}
53+
}
54+
return this.nested.Len() > 0
55+
}

0 commit comments

Comments
 (0)