diff --git "a/0339.\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214/0339-\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214.py" "b/0339.\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214/0339-\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214.py" index 392f5fa..a0348f0 100644 --- "a/0339.\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214/0339-\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214.py" +++ "b/0339.\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214/0339-\345\265\214\345\245\227\345\210\227\350\241\250\346\235\203\351\207\215\345\222\214.py" @@ -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): @@ -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) \ No newline at end of file diff --git "a/0341.\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/0341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250.py" "b/0341.\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/0341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250.py" new file mode 100644 index 0000000..ab91bcb --- /dev/null +++ "b/0341.\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/0341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250.py" @@ -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 diff --git "a/0364.\345\212\240\346\235\203\345\265\214\345\245\227\345\272\217\345\210\227\345\222\214II/0364-\345\212\240\346\235\203\345\265\214\345\245\227\345\272\217\345\210\227\345\222\214II.py" "b/0364.\345\212\240\346\235\203\345\265\214\345\245\227\345\272\217\345\210\227\345\222\214II/0364-\345\212\240\346\235\203\345\265\214\345\245\227\345\272\217\345\210\227\345\222\214II.py" new file mode 100644 index 0000000..99c2c5b --- /dev/null +++ "b/0364.\345\212\240\346\235\203\345\265\214\345\245\227\345\272\217\345\210\227\345\222\214II/0364-\345\212\240\346\235\203\345\265\214\345\245\227\345\272\217\345\210\227\345\222\214II.py" @@ -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 \ No newline at end of file