Skip to content

Commit a329275

Browse files
committed
leetcode 341
python
1 parent 91e0d46 commit a329275

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
9898
| 287| Medium | [Find the Duplicate Number](leetcode/287-Medium-Find-The-Duplicate-Number/problem.md) | [Python](leetcode/287-Medium-Find-The-Duplicate-Number/answer.py) |
9999
| 297| Hard | [Serialize and Deserialize Binary Tree](leetcode/297-Hard-Serialize-And-Deserialize-Binary-Tree/problem.md) | [Java](leetcode/297-Hard-Serialize-And-Deserialize-Binary-Tree/answer.java) |
100100
| 310| Medium | [Minimum Height Trees](leetcode/310-Medium-Minimum-Height-Trees/problem.md) | [Python](leetcode/310-Medium-Minimum-Height-Trees/answer.py) |
101+
| 341| Medium | [Flatten Nested List Iterator](leetcode/341-Medium-Flatten-Nested-List-Iterator/problem.md) | [Python](leetcode/341-Medium-Flatten-Nested-List-Iterator/answer.py) |
101102
| 461| Easy | [Hamming Distance](leetcode/461-Easy-Hamming-Distance/problem.md) | [Python](leetcode/461-Easy-Hamming-Distance/answer.py) |
102103
| 495| Medium | [Teemo Attacking](leetcode/495-Medium-Teemo-Attacking/problem.md) | [Python](leetcode/495-Medium-Teemo-Attacking/answer.py) |
103104
| 733| Easy | [Flood Fill](leetcode/733-Easy-Flood-Fill/problem.md) | [Python](leetcode/733-Easy-Flood-Fill/answer.py) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
3+
#-------------------------------------------------------------------------------
4+
# """
5+
# This is the interface that allows for creating nested lists.
6+
# You should not implement it, or speculate about its implementation
7+
# """
8+
#class NestedInteger(object):
9+
# def isInteger(self):
10+
# """
11+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
12+
# :rtype bool
13+
# """
14+
#
15+
# def getInteger(self):
16+
# """
17+
# @return the single integer that this NestedInteger holds, if it holds a single integer
18+
# Return None if this NestedInteger holds a nested list
19+
# :rtype int
20+
# """
21+
#
22+
# def getList(self):
23+
# """
24+
# @return the nested list that this NestedInteger holds, if it holds a nested list
25+
# Return None if this NestedInteger holds a single integer
26+
# :rtype List[NestedInteger]
27+
# """
28+
29+
class NestedIterator(object):
30+
31+
def __init__(self, nestedList):
32+
"""
33+
Initialize your data structure here.
34+
:type nestedList: List[NestedInteger]
35+
"""
36+
self.flatlist = []
37+
self.idx = 0
38+
self.addLists(nestedList)
39+
40+
41+
def next(self):
42+
"""
43+
:rtype: int
44+
"""
45+
if self.hasNext:
46+
self.idx += 1
47+
return self.flatlist[self.idx-1]
48+
49+
def hasNext(self):
50+
"""
51+
:rtype: bool
52+
"""
53+
return self.idx < len(self.flatlist)
54+
55+
def addLists(self, nest):
56+
for element in nest:
57+
if not element.isInteger():
58+
self.addLists(element.getList())
59+
else:
60+
self.flatlist.append(element.getInteger())
61+
62+
63+
# Your NestedIterator object will be instantiated and called as such:
64+
# i, v = NestedIterator(nestedList), []
65+
# while i.hasNext(): v.append(i.next())
66+
#-------------------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Problem 341: Flatten Nested List Iterator
2+
3+
#### Difficulty: Medium
4+
5+
#### Problem
6+
7+
Given a nested list of integers, implement an iterator to flatten it.
8+
9+
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
10+
11+
#### Example
12+
13+
<pre>
14+
Example 1:
15+
16+
Input: [[1,1],2,[1,1]]
17+
Output: [1,1,2,1,1]
18+
Explanation: By calling next repeatedly until hasNext returns false,
19+
the order of elements returned by next should be: [1,1,2,1,1].
20+
Example 2:
21+
22+
Input: [1,[4,[6]]]
23+
Output: [1,4,6]
24+
Explanation: By calling next repeatedly until hasNext returns false,
25+
the order of elements returned by next should be: [1,4,6].
26+
</pre>

0 commit comments

Comments
 (0)