Skip to content

Commit

Permalink
2019-07-01
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Jul 1, 2019
1 parent 7548689 commit c5aeb36
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
5 changes: 2 additions & 3 deletions 0339.嵌套列表权重和/0339-嵌套列表权重和.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """
# """

class Solution(object):
def depthSum(self, nestedList):
Expand All @@ -50,6 +50,5 @@ def depthSum(self, nestedList):
def Sum(weight, l):
if l.isInteger():
return weight * l.getInteger()
return sum(Sum(weight + 1, item) for item in l.getList())

return sum(Sum(weight + 1, i) for i in l.getList())
return sum(Sum(1, i) for i in nestedList)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger(object):
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """

class NestedIterator(object):

def __init__(self, nestedList):
"""
Initialize your data structure here.
:type nestedList: List[NestedInteger]
"""
if nestedList:
self.stack = nestedList[::-1]
else:
self.stack = []

def next(self):
"""
:rtype: int
"""
return self.stack.pop()

def hasNext(self):
"""
:rtype: bool
"""
if self.stack:
top = self.stack.pop()
while not top.isInteger():
self.stack += top.getList()[::-1]
if self.stack:
top = self.stack.pop()
else:
return False
self.stack.append(top)
return True
else:
return False
69 changes: 69 additions & 0 deletions 0364.加权嵌套序列和II/0364-加权嵌套序列和II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger(object):
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """
#
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """
#
# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """

class Solution(object):
def depthSumInverse(self, nestedList):
"""
:type nestedList: List[NestedInteger]
:rtype: int
"""
record = [0 for _ in range(10000)]
res = 0
self.max_weight = 0

def work(weight, l):
self.max_weight = max(weight, self.max_weight)
if l.isInteger():
record[weight] += l.getInteger()
# print stack
else:
for item in l.getList():
work(weight + 1, item)

for item in nestedList:
work(1, item)

for i in range(1, self.max_weight + 1):
res += (self.max_weight + 1 - i) * record[i]

return res

0 comments on commit c5aeb36

Please sign in to comment.