Skip to content

Commit 0c51902

Browse files
committed
Add solution 341
1 parent fec98db commit 0c51902

26 files changed

+762
-453
lines changed

README.md

Lines changed: 324 additions & 324 deletions
Large diffs are not rendered by default.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package leetcode
2+
3+
import "container/list"
4+
5+
// This is the interface that allows for creating nested lists.
6+
// You should not implement it, or speculate about its implementation
7+
type NestedInteger struct {
8+
}
9+
10+
// Return true if this NestedInteger holds a single integer, rather than a nested list.
11+
func (this NestedInteger) IsInteger() bool { return true }
12+
13+
// Return the single integer that this NestedInteger holds, if it holds a single integer
14+
// The result is undefined if this NestedInteger holds a nested list
15+
// So before calling this method, you should have a check
16+
func (this NestedInteger) GetInteger() int { return 0 }
17+
18+
// Set this NestedInteger to hold a single integer.
19+
func (n *NestedInteger) SetInteger(value int) {}
20+
21+
// Set this NestedInteger to hold a nested list and adds a nested integer to it.
22+
func (this *NestedInteger) Add(elem NestedInteger) {}
23+
24+
// Return the nested list that this NestedInteger holds, if it holds a nested list
25+
// The list length is zero if this NestedInteger holds a single integer
26+
// You can access NestedInteger's List element directly if you want to modify it
27+
func (this NestedInteger) GetList() []*NestedInteger { return nil }
28+
29+
type NestedIterator struct {
30+
stack *list.List
31+
}
32+
33+
type listIndex struct {
34+
nestedList []*NestedInteger
35+
index int
36+
}
37+
38+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
39+
stack := list.New()
40+
stack.PushBack(&listIndex{nestedList, 0})
41+
return &NestedIterator{stack}
42+
}
43+
44+
func (this *NestedIterator) Next() int {
45+
if !this.HasNext() {
46+
return -1
47+
}
48+
last := this.stack.Back().Value.(*listIndex)
49+
nestedList, i := last.nestedList, last.index
50+
val := nestedList[i].GetInteger()
51+
last.index++
52+
return val
53+
}
54+
55+
func (this *NestedIterator) HasNext() bool {
56+
stack := this.stack
57+
for stack.Len() > 0 {
58+
last := stack.Back().Value.(*listIndex)
59+
nestedList, i := last.nestedList, last.index
60+
if i >= len(nestedList) {
61+
stack.Remove(stack.Back())
62+
} else {
63+
val := nestedList[i]
64+
if val.IsInteger() {
65+
return true
66+
}
67+
last.index++
68+
stack.PushBack(&listIndex{val.GetList(), 0})
69+
}
70+
}
71+
return false
72+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func Test_Problem338(t *testing.T) {
9+
obj := Constructor([]*NestedInteger{})
10+
fmt.Printf("obj = %v\n", obj)
11+
fmt.Printf("obj = %v\n", obj.Next())
12+
fmt.Printf("obj = %v\n", obj.HasNext())
13+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# [341. Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)
2+
3+
4+
## 题目
5+
6+
Given a nested list of integers, implement an iterator to flatten it.
7+
8+
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
9+
10+
**Example 1:**
11+
12+
```
13+
Input:[[1,1],2,[1,1]]
14+
Output:[1,1,2,1,1]
15+
Explanation:By callingnext repeatedly untilhasNext returns false,
16+
  the order of elements returned bynext should be:[1,1,2,1,1].
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input:[1,[4,[6]]]
23+
Output:[1,4,6]
24+
Explanation:By callingnext repeatedly untilhasNext returns false,
25+
  the order of elements returned bynext should be:[1,4,6].
26+
27+
```
28+
29+
## 题目大意
30+
31+
给你一个嵌套的整型列表。请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数。列表中的每一项或者为一个整数,或者是另一个列表。其中列表的元素也可能是整数或是其他列表。
32+
33+
## 解题思路
34+
35+
- 题目要求实现一个嵌套版的数组。可以用 `[]int` 实现,也可以用链表实现。笔者此处用链表实现。外层构造一个一维数组,一维数组内部每个元素是一个链表。额外还需要记录这个嵌套链表在原数组中的 `index` 索引。`Next()` 实现比较简单,取出对应的嵌套节点。`HasNext()` 方法则感觉嵌套节点里面的 `index` 信息判断是否还有 `next` 元素。
36+
37+
## 代码
38+
39+
```go
40+
/**
41+
* // This is the interface that allows for creating nested lists.
42+
* // You should not implement it, or speculate about its implementation
43+
* type NestedInteger struct {
44+
* }
45+
*
46+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
47+
* func (this NestedInteger) IsInteger() bool {}
48+
*
49+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
50+
* // The result is undefined if this NestedInteger holds a nested list
51+
* // So before calling this method, you should have a check
52+
* func (this NestedInteger) GetInteger() int {}
53+
*
54+
* // Set this NestedInteger to hold a single integer.
55+
* func (n *NestedInteger) SetInteger(value int) {}
56+
*
57+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
58+
* func (this *NestedInteger) Add(elem NestedInteger) {}
59+
*
60+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
61+
* // The list length is zero if this NestedInteger holds a single integer
62+
* // You can access NestedInteger's List element directly if you want to modify it
63+
* func (this NestedInteger) GetList() []*NestedInteger {}
64+
*/
65+
66+
type NestedIterator struct {
67+
stack *list.List
68+
}
69+
70+
type listIndex struct {
71+
nestedList []*NestedInteger
72+
index int
73+
}
74+
75+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
76+
stack := list.New()
77+
stack.PushBack(&listIndex{nestedList, 0})
78+
return &NestedIterator{stack}
79+
}
80+
81+
func (this *NestedIterator) Next() int {
82+
if !this.HasNext() {
83+
return -1
84+
}
85+
last := this.stack.Back().Value.(*listIndex)
86+
nestedList, i := last.nestedList, last.index
87+
val := nestedList[i].GetInteger()
88+
last.index++
89+
return val
90+
}
91+
92+
func (this *NestedIterator) HasNext() bool {
93+
stack := this.stack
94+
for stack.Len() > 0 {
95+
last := stack.Back().Value.(*listIndex)
96+
nestedList, i := last.nestedList, last.index
97+
if i >= len(nestedList) {
98+
stack.Remove(stack.Back())
99+
} else {
100+
val := nestedList[i]
101+
if val.IsInteger() {
102+
return true
103+
}
104+
last.index++
105+
stack.PushBack(&listIndex{val.GetList(), 0})
106+
}
107+
}
108+
return false
109+
}
110+
```

leetcode/0456.132-Pattern/456. 132 Pattern.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package leetcode
22

33
import (
4-
"fmt"
54
"math"
65
)
76

@@ -20,7 +19,6 @@ func find132pattern(nums []int) bool {
2019
stack = stack[:len(stack)-1]
2120
}
2221
stack = append(stack, nums[i])
23-
fmt.Printf("stack = %v \n", stack)
2422
}
2523
return false
2624
}

website/content/ChapterFour/0300~0399/0338.Counting-Bits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ func countBits(num int) []int {
5757
----------------------------------------------
5858
<div style="display: flex;justify-content: space-between;align-items: center;">
5959
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0337.House-Robber-III/">⬅️上一页</a></p>
60-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0342.Power-of-Four/">下一页➡️</a></p>
60+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0341.Flatten-Nested-List-Iterator/">下一页➡️</a></p>
6161
</div>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# [341. Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)
2+
3+
4+
## 题目
5+
6+
Given a nested list of integers, implement an iterator to flatten it.
7+
8+
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
9+
10+
**Example 1:**
11+
12+
```
13+
Input:[[1,1],2,[1,1]]
14+
Output:[1,1,2,1,1]
15+
Explanation:By callingnext repeatedly untilhasNext returns false,
16+
  the order of elements returned bynext should be:[1,1,2,1,1].
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input:[1,[4,[6]]]
23+
Output:[1,4,6]
24+
Explanation:By callingnext repeatedly untilhasNext returns false,
25+
  the order of elements returned bynext should be:[1,4,6].
26+
27+
```
28+
29+
## 题目大意
30+
31+
给你一个嵌套的整型列表。请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数。列表中的每一项或者为一个整数,或者是另一个列表。其中列表的元素也可能是整数或是其他列表。
32+
33+
## 解题思路
34+
35+
- 题目要求实现一个嵌套版的数组。可以用 `[]int` 实现,也可以用链表实现。笔者此处用链表实现。外层构造一个一维数组,一维数组内部每个元素是一个链表。额外还需要记录这个嵌套链表在原数组中的 `index` 索引。`Next()` 实现比较简单,取出对应的嵌套节点。`HasNext()` 方法则感觉嵌套节点里面的 `index` 信息判断是否还有 `next` 元素。
36+
37+
## 代码
38+
39+
```go
40+
/**
41+
* // This is the interface that allows for creating nested lists.
42+
* // You should not implement it, or speculate about its implementation
43+
* type NestedInteger struct {
44+
* }
45+
*
46+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
47+
* func (this NestedInteger) IsInteger() bool {}
48+
*
49+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
50+
* // The result is undefined if this NestedInteger holds a nested list
51+
* // So before calling this method, you should have a check
52+
* func (this NestedInteger) GetInteger() int {}
53+
*
54+
* // Set this NestedInteger to hold a single integer.
55+
* func (n *NestedInteger) SetInteger(value int) {}
56+
*
57+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
58+
* func (this *NestedInteger) Add(elem NestedInteger) {}
59+
*
60+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
61+
* // The list length is zero if this NestedInteger holds a single integer
62+
* // You can access NestedInteger's List element directly if you want to modify it
63+
* func (this NestedInteger) GetList() []*NestedInteger {}
64+
*/
65+
66+
type NestedIterator struct {
67+
stack *list.List
68+
}
69+
70+
type listIndex struct {
71+
nestedList []*NestedInteger
72+
index int
73+
}
74+
75+
func Constructor(nestedList []*NestedInteger) *NestedIterator {
76+
stack := list.New()
77+
stack.PushBack(&listIndex{nestedList, 0})
78+
return &NestedIterator{stack}
79+
}
80+
81+
func (this *NestedIterator) Next() int {
82+
if !this.HasNext() {
83+
return -1
84+
}
85+
last := this.stack.Back().Value.(*listIndex)
86+
nestedList, i := last.nestedList, last.index
87+
val := nestedList[i].GetInteger()
88+
last.index++
89+
return val
90+
}
91+
92+
func (this *NestedIterator) HasNext() bool {
93+
stack := this.stack
94+
for stack.Len() > 0 {
95+
last := stack.Back().Value.(*listIndex)
96+
nestedList, i := last.nestedList, last.index
97+
if i >= len(nestedList) {
98+
stack.Remove(stack.Back())
99+
} else {
100+
val := nestedList[i]
101+
if val.IsInteger() {
102+
return true
103+
}
104+
last.index++
105+
stack.PushBack(&listIndex{val.GetList(), 0})
106+
}
107+
}
108+
return false
109+
}
110+
```
111+
112+
113+
----------------------------------------------
114+
<div style="display: flex;justify-content: space-between;align-items: center;">
115+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0338.Counting-Bits/">⬅️上一页</a></p>
116+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0342.Power-of-Four/">下一页➡️</a></p>
117+
</div>

website/content/ChapterFour/0300~0399/0342.Power-of-Four.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ func isPowerOfFour1(num int) bool {
5757

5858
----------------------------------------------
5959
<div style="display: flex;justify-content: space-between;align-items: center;">
60-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0338.Counting-Bits/">⬅️上一页</a></p>
60+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0341.Flatten-Nested-List-Iterator/">⬅️上一页</a></p>
6161
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0343.Integer-Break/">下一页➡️</a></p>
6262
</div>

website/content/ChapterFour/0400~0499/0456.132-Pattern.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence a
5353
package leetcode
5454

5555
import (
56-
"fmt"
5756
"math"
5857
)
5958

@@ -72,7 +71,6 @@ func find132pattern(nums []int) bool {
7271
stack = stack[:len(stack)-1]
7372
}
7473
stack = append(stack, nums[i])
75-
fmt.Printf("stack = %v \n", stack)
7674
}
7775
return false
7876
}

0 commit comments

Comments
 (0)