Skip to content

Commit b6877ce

Browse files
committed
solve problem Flatten Nested List Iterator
1 parent e62610b commit b6877ce

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ All solutions will be accepted!
267267
|526|[Beautiful Arrangement](https://leetcode-cn.com/problems/beautiful-arrangement/description/)|[java/py/js](./algorithms/BeautifulArrangement)|Medium|
268268
|814|[Binary Tree Pruning](https://leetcode-cn.com/problems/binary-tree-pruning/description/)|[java/py/js](./algorithms/BinaryTreePruning)|Medium|
269269
|150|[Evaluate Reverse Polish Notation](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/description/)|[java/py/js](./algorithms/EvaluateReversePolishNotation)|Medium|
270+
|341|[Flatten Nested List Iterator](https://leetcode-cn.com/problems/flatten-nested-list-iterator/description/)|[java/py/js](./algorithms/FlattenNestedListIterator)|Medium|
270271

271272
# Database
272273
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Flatten Nested List Iterator
2+
This problem is easy to solve by stack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
private LinkedList<Integer> stack;
20+
21+
public NestedIterator(List<NestedInteger> nestedList) {
22+
stack = new LinkedList<Integer>();
23+
while (nestedList.size() > 0) {
24+
NestedInteger d = nestedList.remove(nestedList.size() - 1);
25+
if (d.isInteger())
26+
stack.push(d.getInteger());
27+
else
28+
nestedList.addAll(d.getList());
29+
}
30+
}
31+
32+
@Override
33+
public Integer next() {
34+
return stack.pop();
35+
}
36+
37+
@Override
38+
public boolean hasNext() {
39+
return stack.size() > 0;
40+
}
41+
}
42+
43+
/**
44+
* Your NestedIterator object will be instantiated and called as such:
45+
* NestedIterator i = new NestedIterator(nestedList);
46+
* while (i.hasNext()) v[f()] = i.next();
47+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* function NestedInteger() {
5+
*
6+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
7+
* @return {boolean}
8+
* this.isInteger = function() {
9+
* ...
10+
* };
11+
*
12+
* Return the single integer that this NestedInteger holds, if it holds a single integer
13+
* Return null if this NestedInteger holds a nested list
14+
* @return {integer}
15+
* this.getInteger = function() {
16+
* ...
17+
* };
18+
*
19+
* Return the nested list that this NestedInteger holds, if it holds a nested list
20+
* Return null if this NestedInteger holds a single integer
21+
* @return {NestedInteger[]}
22+
* this.getList = function() {
23+
* ...
24+
* };
25+
* };
26+
*/
27+
/**
28+
* @constructor
29+
* @param {NestedInteger[]} nestedList
30+
*/
31+
var NestedIterator = function(nestedList) {
32+
this.stack = []
33+
while (nestedList.length > 0) {
34+
let d = nestedList.pop()
35+
if (d.isInteger())
36+
this.stack.push(d.getInteger())
37+
else
38+
nestedList = nestedList.concat(d.getList())
39+
}
40+
};
41+
42+
43+
/**
44+
* @this NestedIterator
45+
* @returns {boolean}
46+
*/
47+
NestedIterator.prototype.hasNext = function() {
48+
return this.stack.length > 0
49+
};
50+
51+
/**
52+
* @this NestedIterator
53+
* @returns {integer}
54+
*/
55+
NestedIterator.prototype.next = function() {
56+
return this.stack.pop()
57+
};
58+
59+
/**
60+
* Your NestedIterator will be called like this:
61+
* var i = new NestedIterator(nestedList), a = [];
62+
* while (i.hasNext()) a.push(i.next());
63+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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(object):
6+
# def isInteger(self):
7+
# """
8+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
9+
# :rtype bool
10+
# """
11+
#
12+
# def getInteger(self):
13+
# """
14+
# @return the single integer that this NestedInteger holds, if it holds a single integer
15+
# Return None if this NestedInteger holds a nested list
16+
# :rtype int
17+
# """
18+
#
19+
# def getList(self):
20+
# """
21+
# @return the nested list that this NestedInteger holds, if it holds a nested list
22+
# Return None if this NestedInteger holds a single integer
23+
# :rtype List[NestedInteger]
24+
# """
25+
26+
class NestedIterator(object):
27+
28+
def __init__(self, nestedList):
29+
"""
30+
Initialize your data structure here.
31+
:type nestedList: List[NestedInteger]
32+
"""
33+
self.stack = []
34+
while len(nestedList) > 0:
35+
d = nestedList.pop()
36+
if d.isInteger():
37+
self.stack.append(d.getInteger())
38+
else:
39+
nestedList += d.getList()
40+
41+
def next(self):
42+
"""
43+
:rtype: int
44+
"""
45+
return self.stack.pop()
46+
47+
48+
def hasNext(self):
49+
"""
50+
:rtype: bool
51+
"""
52+
return len(self.stack) > 0
53+
54+
# Your NestedIterator object will be instantiated and called as such:
55+
# i, v = NestedIterator(nestedList), []
56+
# while i.hasNext(): v.append(i.next())

0 commit comments

Comments
 (0)