how to re-write the following codes in Python reduce?


  • 0
    C
    class Solution(object):
        def depthSum(self, nestedList, depth=1):
            """
            :type nestedList: List[NestedInteger]
            :rtype: int
            """
            if nestedList is None:
                return 0
            if isinstance(nestedList, list):
                total = 0
                for lst in nestedList:
                    total += self.depthSum(lst, depth)
                return total
            elif isinstance(nestedList, NestedInteger) and nestedList.isInteger():
                return depth*nestedList.getInteger()
            else:
                total = 0
                for lst in nestedList.getList():
                    total += self.depthSum(lst, depth+1)
                return total
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.