Skip to content

Commit 95c5912

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0341
1 parent 4a46446 commit 95c5912

File tree

4 files changed

+303
-4
lines changed

4 files changed

+303
-4
lines changed

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

+101-2
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,114 @@
3535
<!-- 这里可写当前语言的特殊实现逻辑 -->
3636

3737
```python
38-
38+
# """
39+
# This is the interface that allows for creating nested lists.
40+
# You should not implement it, or speculate about its implementation
41+
# """
42+
#class NestedInteger:
43+
# def isInteger(self) -> bool:
44+
# """
45+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
46+
# """
47+
#
48+
# def getInteger(self) -> int:
49+
# """
50+
# @return the single integer that this NestedInteger holds, if it holds a single integer
51+
# Return None if this NestedInteger holds a nested list
52+
# """
53+
#
54+
# def getList(self) -> [NestedInteger]:
55+
# """
56+
# @return the nested list that this NestedInteger holds, if it holds a nested list
57+
# Return None if this NestedInteger holds a single integer
58+
# """
59+
60+
class NestedIterator:
61+
def __init__(self, nestedList: [NestedInteger]):
62+
def dfs(nestedList):
63+
for e in nestedList:
64+
if e.isInteger():
65+
self.vals.append(e.getInteger())
66+
else:
67+
dfs(e.getList())
68+
69+
self.vals = []
70+
dfs(nestedList)
71+
self.cur = 0
72+
73+
def next(self) -> int:
74+
res = self.vals[self.cur]
75+
self.cur += 1
76+
return res
77+
78+
def hasNext(self) -> bool:
79+
return self.cur < len(self.vals)
80+
81+
# Your NestedIterator object will be instantiated and called as such:
82+
# i, v = NestedIterator(nestedList), []
83+
# while i.hasNext(): v.append(i.next())
3984
```
4085

4186
### **Java**
4287

4388
<!-- 这里可写当前语言的特殊实现逻辑 -->
4489

4590
```java
46-
91+
/**
92+
* // This is the interface that allows for creating nested lists.
93+
* // You should not implement it, or speculate about its implementation
94+
* public interface NestedInteger {
95+
*
96+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
97+
* public boolean isInteger();
98+
*
99+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
100+
* // Return null if this NestedInteger holds a nested list
101+
* public Integer getInteger();
102+
*
103+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
104+
* // Return null if this NestedInteger holds a single integer
105+
* public List<NestedInteger> getList();
106+
* }
107+
*/
108+
public class NestedIterator implements Iterator<Integer> {
109+
110+
private List<Integer> vals;
111+
112+
private Iterator<Integer> cur;
113+
114+
public NestedIterator(List<NestedInteger> nestedList) {
115+
vals = new ArrayList<>();
116+
dfs(nestedList);
117+
cur = vals.iterator();
118+
}
119+
120+
@Override
121+
public Integer next() {
122+
return cur.next();
123+
}
124+
125+
@Override
126+
public boolean hasNext() {
127+
return cur.hasNext();
128+
}
129+
130+
private void dfs(List<NestedInteger> nestedList) {
131+
for (NestedInteger e : nestedList) {
132+
if (e.isInteger()) {
133+
vals.add(e.getInteger());
134+
} else {
135+
dfs(e.getList());
136+
}
137+
}
138+
}
139+
}
140+
141+
/**
142+
* Your NestedIterator object will be instantiated and called as such:
143+
* NestedIterator i = new NestedIterator(nestedList);
144+
* while (i.hasNext()) v[f()] = i.next();
145+
*/
47146
```
48147

49148
### **...**

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

+101-2
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,112 @@
4949
### **Python3**
5050

5151
```python
52-
52+
# """
53+
# This is the interface that allows for creating nested lists.
54+
# You should not implement it, or speculate about its implementation
55+
# """
56+
#class NestedInteger:
57+
# def isInteger(self) -> bool:
58+
# """
59+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
60+
# """
61+
#
62+
# def getInteger(self) -> int:
63+
# """
64+
# @return the single integer that this NestedInteger holds, if it holds a single integer
65+
# Return None if this NestedInteger holds a nested list
66+
# """
67+
#
68+
# def getList(self) -> [NestedInteger]:
69+
# """
70+
# @return the nested list that this NestedInteger holds, if it holds a nested list
71+
# Return None if this NestedInteger holds a single integer
72+
# """
73+
74+
class NestedIterator:
75+
def __init__(self, nestedList: [NestedInteger]):
76+
def dfs(nestedList):
77+
for e in nestedList:
78+
if e.isInteger():
79+
self.vals.append(e.getInteger())
80+
else:
81+
dfs(e.getList())
82+
83+
self.vals = []
84+
dfs(nestedList)
85+
self.cur = 0
86+
87+
def next(self) -> int:
88+
res = self.vals[self.cur]
89+
self.cur += 1
90+
return res
91+
92+
def hasNext(self) -> bool:
93+
return self.cur < len(self.vals)
94+
95+
# Your NestedIterator object will be instantiated and called as such:
96+
# i, v = NestedIterator(nestedList), []
97+
# while i.hasNext(): v.append(i.next())
5398
```
5499

55100
### **Java**
56101

57102
```java
58-
103+
/**
104+
* // This is the interface that allows for creating nested lists.
105+
* // You should not implement it, or speculate about its implementation
106+
* public interface NestedInteger {
107+
*
108+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
109+
* public boolean isInteger();
110+
*
111+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
112+
* // Return null if this NestedInteger holds a nested list
113+
* public Integer getInteger();
114+
*
115+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
116+
* // Return null if this NestedInteger holds a single integer
117+
* public List<NestedInteger> getList();
118+
* }
119+
*/
120+
public class NestedIterator implements Iterator<Integer> {
121+
122+
private List<Integer> vals;
123+
124+
private Iterator<Integer> cur;
125+
126+
public NestedIterator(List<NestedInteger> nestedList) {
127+
vals = new ArrayList<>();
128+
dfs(nestedList);
129+
cur = vals.iterator();
130+
}
131+
132+
@Override
133+
public Integer next() {
134+
return cur.next();
135+
}
136+
137+
@Override
138+
public boolean hasNext() {
139+
return cur.hasNext();
140+
}
141+
142+
private void dfs(List<NestedInteger> nestedList) {
143+
for (NestedInteger e : nestedList) {
144+
if (e.isInteger()) {
145+
vals.add(e.getInteger());
146+
} else {
147+
dfs(e.getList());
148+
}
149+
}
150+
}
151+
}
152+
153+
/**
154+
* Your NestedIterator object will be instantiated and called as such:
155+
* NestedIterator i = new NestedIterator(nestedList);
156+
* while (i.hasNext()) v[f()] = i.next();
157+
*/
59158
```
60159

61160
### **...**
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+
* public interface NestedInteger {
5+
*
6+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
7+
* public boolean isInteger();
8+
*
9+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
10+
* // Return null if this NestedInteger holds a nested list
11+
* public Integer getInteger();
12+
*
13+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
14+
* // Return null if this NestedInteger holds a single integer
15+
* public List<NestedInteger> getList();
16+
* }
17+
*/
18+
public class NestedIterator implements Iterator<Integer> {
19+
20+
private List<Integer> vals;
21+
22+
private Iterator<Integer> cur;
23+
24+
public NestedIterator(List<NestedInteger> nestedList) {
25+
vals = new ArrayList<>();
26+
dfs(nestedList);
27+
cur = vals.iterator();
28+
}
29+
30+
@Override
31+
public Integer next() {
32+
return cur.next();
33+
}
34+
35+
@Override
36+
public boolean hasNext() {
37+
return cur.hasNext();
38+
}
39+
40+
private void dfs(List<NestedInteger> nestedList) {
41+
for (NestedInteger e : nestedList) {
42+
if (e.isInteger()) {
43+
vals.add(e.getInteger());
44+
} else {
45+
dfs(e.getList());
46+
}
47+
}
48+
}
49+
}
50+
51+
/**
52+
* Your NestedIterator object will be instantiated and called as such:
53+
* NestedIterator i = new NestedIterator(nestedList);
54+
* while (i.hasNext()) v[f()] = i.next();
55+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# """
2+
# This is the interface that allows for creating nested lists.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
#class NestedInteger:
6+
# def isInteger(self) -> bool:
7+
# """
8+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
9+
# """
10+
#
11+
# def getInteger(self) -> int:
12+
# """
13+
# @return the single integer that this NestedInteger holds, if it holds a single integer
14+
# Return None if this NestedInteger holds a nested list
15+
# """
16+
#
17+
# def getList(self) -> [NestedInteger]:
18+
# """
19+
# @return the nested list that this NestedInteger holds, if it holds a nested list
20+
# Return None if this NestedInteger holds a single integer
21+
# """
22+
23+
class NestedIterator:
24+
def __init__(self, nestedList: [NestedInteger]):
25+
def dfs(nestedList):
26+
for e in nestedList:
27+
if e.isInteger():
28+
self.vals.append(e.getInteger())
29+
else:
30+
dfs(e.getList())
31+
32+
self.vals = []
33+
dfs(nestedList)
34+
self.cur = 0
35+
36+
def next(self) -> int:
37+
res = self.vals[self.cur]
38+
self.cur += 1
39+
return res
40+
41+
def hasNext(self) -> bool:
42+
return self.cur < len(self.vals)
43+
44+
# Your NestedIterator object will be instantiated and called as such:
45+
# i, v = NestedIterator(nestedList), []
46+
# while i.hasNext(): v.append(i.next())

0 commit comments

Comments
 (0)