Skip to content

Commit d50d9b2

Browse files
committed
Add solution and test-cases for problem 341
1 parent 7b6b935 commit d50d9b2

File tree

3 files changed

+104
-20
lines changed

3 files changed

+104
-20
lines changed

leetcode/301-400/0341.Flatten-Nested-List-Iterator/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
# [341.Flatten Nested List Iterator][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a nested list of integers `nestedList`. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
75

8-
**Example 1:**
6+
Implement the `NestedIterator` class:
97

8+
- `NestedIterator(List<NestedInteger> nestedList)` Initializes the iterator with the nested list `nestedList`.
9+
- `int next()` Returns the next integer in the nested list.
10+
- `boolean hasNext()` Returns `true` if there are still some integers in the nested list and `false` otherwise.
11+
Your code will be tested with the following pseudocode:
1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
initialize iterator with nestedList
14+
res = []
15+
while iterator.hasNext()
16+
append iterator.next() to the end of res
17+
return res
1318
```
19+
If `res` matches the expected flattened list, then your code will be judged as correct.
1420

15-
## 题意
16-
> ...
21+
**Example 1:**
1722

18-
## 题解
23+
```
24+
Input: nestedList = [[1,1],2,[1,1]]
25+
Output: [1,1,2,1,1]
26+
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
27+
```
1928

20-
### 思路1
21-
> ...
22-
Flatten Nested List Iterator
23-
```go
29+
__Example 2:__
30+
```
31+
Input: nestedList = [1,[4,[6]]]
32+
Output: [1,4,6]
33+
Explanation: By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
2434
```
2535

2636

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type NestedInteger struct {
4+
Val int
5+
isInt bool
6+
list []*NestedInteger
7+
}
8+
9+
func (this NestedInteger) IsInteger() bool {
10+
return this.isInt
11+
}
12+
13+
func (this NestedInteger) GetInteger() int {
14+
return this.Val
15+
}
16+
17+
func (this NestedInteger) SetInteger(value int) {
18+
19+
}
20+
func (this NestedInteger) Add(ele NestedInteger) {
21+
22+
}
23+
24+
func (this NestedInteger) GetList() []*NestedInteger {
25+
return this.list
26+
}
27+
28+
type NestedIterator struct {
29+
Raw []*NestedInteger
30+
idx int
31+
}
32+
33+
func Constructor3(nestedList []*NestedInteger) *NestedIterator {
34+
raw := make([]*NestedInteger, 0)
35+
deepTraverse(nestedList, &raw)
36+
return &NestedIterator{
37+
Raw: raw,
38+
idx: 0,
39+
}
40+
}
41+
42+
func deepTraverse(nestedList []*NestedInteger, raw *[]*NestedInteger) {
43+
for idx, ele := range nestedList {
44+
if ele.IsInteger() {
45+
*raw = append(*raw, nestedList[idx])
46+
continue
47+
}
48+
deepTraverse(ele.GetList(), raw)
49+
}
50+
}
51+
52+
func (this *NestedIterator) Next() int {
53+
r := this.Raw[this.idx].GetInteger()
54+
this.idx++
55+
return r
56+
}
57+
58+
func (this *NestedIterator) HasNext() bool {
59+
return this.idx < len(this.Raw)
60+
}
61+
62+
func Solution(nestedList []*NestedInteger) []int {
63+
o := Constructor3(nestedList)
64+
expect := make([]int, 0)
65+
for o.HasNext() {
66+
expect = append(expect, o.Next())
67+
}
68+
return expect
569
}

leetcode/301-400/0341.Flatten-Nested-List-Iterator/Solution_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []*NestedInteger
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []*NestedInteger{{Val: 1, isInt: true}, {isInt: false, list: []*NestedInteger{{Val: 2, isInt: true}, {Val: 3, isInt: true}}}, {Val: 1, isInt: true, list: nil}}, []int{1, 2, 3, 1}},
17+
{"TestCase2", []*NestedInteger{{Val: 1, isInt: true}, {Val: 2, isInt: true}, {Val: 3, isInt: true}}, []int{1, 2, 3}},
18+
{"TestCase3", []*NestedInteger{
19+
{Val: 1, isInt: true},
20+
{isInt: false, list: []*NestedInteger{
21+
{isInt: false, list: []*NestedInteger{{Val: 2, isInt: true}}},
22+
{isInt: false, list: []*NestedInteger{
23+
{Val: 3, isInt: true},
24+
{Val: 4, isInt: true},
25+
}},
26+
}},
27+
{Val: 5, isInt: true}},
28+
[]int{1, 2, 3, 4, 5}},
1929
}
2030

2131
// 开始测试

0 commit comments

Comments
 (0)