Skip to content

Commit c5aeb36

Browse files
committed
2019-07-01
1 parent 7548689 commit c5aeb36

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

0339.嵌套列表权重和/0339-嵌套列表权重和.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# @return the nested list that this NestedInteger holds, if it holds a nested list
4040
# Return None if this NestedInteger holds a single integer
4141
# :rtype List[NestedInteger]
42-
# """
42+
# """
4343

4444
class Solution(object):
4545
def depthSum(self, nestedList):
@@ -50,6 +50,5 @@ def depthSum(self, nestedList):
5050
def Sum(weight, l):
5151
if l.isInteger():
5252
return weight * l.getInteger()
53-
return sum(Sum(weight + 1, item) for item in l.getList())
54-
53+
return sum(Sum(weight + 1, i) for i in l.getList())
5554
return sum(Sum(1, i) for i in nestedList)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# """
2+
# This is the interface that allows for creating nested lists.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
#class NestedInteger(object):
6+
# def isInteger(self):
7+
# """
8+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
9+
# :rtype bool
10+
# """
11+
#
12+
# def getInteger(self):
13+
# """
14+
# @return the single integer that this NestedInteger holds, if it holds a single integer
15+
# Return None if this NestedInteger holds a nested list
16+
# :rtype int
17+
# """
18+
#
19+
# def getList(self):
20+
# """
21+
# @return the nested list that this NestedInteger holds, if it holds a nested list
22+
# Return None if this NestedInteger holds a single integer
23+
# :rtype List[NestedInteger]
24+
# """
25+
26+
class NestedIterator(object):
27+
28+
def __init__(self, nestedList):
29+
"""
30+
Initialize your data structure here.
31+
:type nestedList: List[NestedInteger]
32+
"""
33+
if nestedList:
34+
self.stack = nestedList[::-1]
35+
else:
36+
self.stack = []
37+
38+
def next(self):
39+
"""
40+
:rtype: int
41+
"""
42+
return self.stack.pop()
43+
44+
def hasNext(self):
45+
"""
46+
:rtype: bool
47+
"""
48+
if self.stack:
49+
top = self.stack.pop()
50+
while not top.isInteger():
51+
self.stack += top.getList()[::-1]
52+
if self.stack:
53+
top = self.stack.pop()
54+
else:
55+
return False
56+
self.stack.append(top)
57+
return True
58+
else:
59+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# """
2+
# This is the interface that allows for creating nested lists.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
#class NestedInteger(object):
6+
# def __init__(self, value=None):
7+
# """
8+
# If value is not specified, initializes an empty list.
9+
# Otherwise initializes a single integer equal to value.
10+
# """
11+
#
12+
# def isInteger(self):
13+
# """
14+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
15+
# :rtype bool
16+
# """
17+
#
18+
# def add(self, elem):
19+
# """
20+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
21+
# :rtype void
22+
# """
23+
#
24+
# def setInteger(self, value):
25+
# """
26+
# Set this NestedInteger to hold a single integer equal to value.
27+
# :rtype void
28+
# """
29+
#
30+
# def getInteger(self):
31+
# """
32+
# @return the single integer that this NestedInteger holds, if it holds a single integer
33+
# Return None if this NestedInteger holds a nested list
34+
# :rtype int
35+
# """
36+
#
37+
# def getList(self):
38+
# """
39+
# @return the nested list that this NestedInteger holds, if it holds a nested list
40+
# Return None if this NestedInteger holds a single integer
41+
# :rtype List[NestedInteger]
42+
# """
43+
44+
class Solution(object):
45+
def depthSumInverse(self, nestedList):
46+
"""
47+
:type nestedList: List[NestedInteger]
48+
:rtype: int
49+
"""
50+
record = [0 for _ in range(10000)]
51+
res = 0
52+
self.max_weight = 0
53+
54+
def work(weight, l):
55+
self.max_weight = max(weight, self.max_weight)
56+
if l.isInteger():
57+
record[weight] += l.getInteger()
58+
# print stack
59+
else:
60+
for item in l.getList():
61+
work(weight + 1, item)
62+
63+
for item in nestedList:
64+
work(1, item)
65+
66+
for i in range(1, self.max_weight + 1):
67+
res += (self.max_weight + 1 - i) * record[i]
68+
69+
return res

0 commit comments

Comments
 (0)