<p>你的解决方案有两个问题。你知道吗</p>
<ol>
<li>在下一个方法中将returnnull更改为None。你知道吗</li>
<li>在flattelist中,ele的大小比较只适用于python2!为它添加len()函数以在python3上工作。你知道吗</li>
</ol>
<p>这是修改后的代码。应该到处都是。你知道吗</p>
<pre><code>class NestedIterator(object):
currIdx = 0
def __init__(self, nestedList):
"""
Initialize your data structure here.
:type nestedList: List[NestedInteger]
"""
newFlattenedList = []
self.flattenList(nestedList, newFlattenedList)
nestedList = newFlattenedList
self.flattenedList = nestedList
def flattenList(self, nestedList, flattenedList):
for ele in nestedList:
if type(ele) == list and len(ele) > 0:
self.flattenList(ele, flattenedList)
else:
flattenedList.append(ele)
return
def next(self):
"""
:rtype: int
"""
if self.hasNext():
test = self.flattenedList[self.currIdx]
self.currIdx +=1
return test
else:
return None
def hasNext(self):
"""
:rtype: bool
"""
nextIdx = self.currIdx + 1
return True if nextIdx <= len(self.flattenedList) else False
</code></pre>