【问题标题】:Flattening a list containing subslists展平包含子列表的列表
【发布时间】:2017-09-22 04:33:17
【问题描述】:

我正在尝试解决问题以通过 Leetcode 上的在线评委。问题是:给定一个嵌套的整数列表,实现一个迭代器来展平它。

每个元素要么是一个整数,要么是一个列表——其元素也可以是整数或其他列表。

示例 1: 给定列表 [[1,1],2,[1,1]],

通过反复调用next直到hasNext返回false,next返回的元素顺序应该是:[1,1,2,1,1]

完整的问题是here

问题说明它将实例化使用以下代码实现的类:

# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())

以下是我的解决方案:

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 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 NULL 

    def hasNext(self):
        """
        :rtype: bool
        """
        nextIdx = self.currIdx + 1 
        return True if nextIdx <= len(self.flattenedList) else False

当我在输入为 [[1,1],2,[1,1]] 的 IDE 中运行此代码时,我得到的输出为 [1,1,2,1,1]。由于某种原因,当我用在线判断运行代码时,给定输入[[1,1],2,[1,1]],输出为[[1,1],2,[1,1] ] 返回。为什么 leetcode 在线裁判返回的东西不一样?

【问题讨论】:

    标签: python list algorithm


    【解决方案1】:

    您的意思是None 不是NULL 对吧?

    def next(self):
        """
        :rtype: int
        """
        if self.hasNext():
    
            test = self.flattenedList[self.currIdx]
            self.currIdx +=1
            return test
        else:
            #return NULL 
            return None
    

    通过运行:

    nestedList = [[1,1],2,[1,1]]
    i, v = NestedIterator(nestedList), []
    while i.hasNext(): v.append(i.next())
    
    print v
    

    我明白了:

    [1, 1, 2, 1, 1]
    

    所以,除了将 NULL 更改为 None,我不知道。

    【讨论】:

      【解决方案2】:

      您的解决方案有 2 个问题。

      1. 在下一个方法中将返回 NULL 更改为 None。
      2. 在 flattenList 中,ele 的大小比较仅适用于 Python 2!添加 len() 函数使其在 Python 3 上工作。

      这是修改后的代码。这应该在任何地方运行。

      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
      

      【讨论】:

      • 当我在leetcode 上提交时它仍然无法正常工作。它对你有用吗?
      猜你喜欢
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 2021-12-03
      • 2019-11-29
      • 1970-01-01
      • 2021-02-27
      相关资源
      最近更新 更多