-
Notifications
You must be signed in to change notification settings - Fork 44
/
nested-list-weight-sum.py
161 lines (156 loc) · 4.5 KB
/
nested-list-weight-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# V0
# IDEA : BFS
class Solution(object):
def depthSum(self, nestedList):
"""
:type nestedList: List[NestedInteger]
:rtype: int
"""
depth, ans = 1, 0
while nestedList:
ans += depth*sum([i.getInteger() for i in nestedList if i.isInteger()])
newList = []
for i in nestedList:
if not i.isInteger():
newList += i.getList()
nestedList = newList
depth += 1
return ans
# V0'
# IDEA : DFS
# class Solution(object):
# def depthSum(self, nestedList):
# d = 1
# r = 0
# for tmp in nestedList:
# self.dfs(nestedList, d, r,)
# return r
#
# def dfs(self, nestedList, d, r)
# if len(nestedList) == 0:
# return 0
# if tmp is isInteger():
# r += d*tmp
# elif tmp == "[":
# self.dfs(nestedList, d+1, r)
# elif tmp == "]":
# self.dfs(nestedList, d-1, r)
# V1
# https://blog.csdn.net/danspace1/article/details/87645153
# IDEA : BFS
# """
#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 depthSum(self, nestedList):
"""
:type nestedList: List[NestedInteger]
:rtype: int
"""
depth, ans = 1, 0
while nestedList:
ans += depth*sum([i.getInteger() for i in nestedList if i.isInteger()])
newList = []
for i in nestedList:
if not i.isInteger():
newList += i.getList()
nestedList = newList
depth += 1
return ans
# V1'
# https://www.jiuzhang.com/solution/nested-list-weight-sum/#tag-highlight-lang-python
class Solution(object):
# @param {NestedInteger[]} nestedList a list of NestedInteger Object
# @return {int} an integer
def depthSum(self, nestedList):
# Write your code here
if len(nestedList) == 0:
return 0
stack = []
sum = 0
for n in nestedList:
stack.append((n, 1))
while stack:
next, d = stack.pop(0)
if next.isInteger():
sum += d * next.getInteger()
else:
for i in next.getList():
stack.append((i, d+1))
return sum
# V1''
# https://www.cnblogs.com/grandyang/p/5340305.html
# JAVA
# IDEA : DFS
# class Solution {
# public:
# int depthSum(vector<NestedInteger>& nestedList) {
# int res = 0;
# for (auto a : nestedList) {
# res += getSum(a, 1);
# }
# return res;
# }
# int getSum(NestedInteger ni, int level) {
# int res = 0;
# if (ni.isInteger()) return level * ni.getInteger();
# for (auto a : ni.getList()) {
# res += getSum(a, level + 1);
# }
# return res;
# }
# };
# V2
# Time: O(n)
# Space: O(h)
class Solution(object):
def depthSum(self, nestedList):
"""
:type nestedList: List[NestedInteger]
:rtype: int
"""
def depthSumHelper(nestedList, depth):
res = 0
for l in nestedList:
if l.isInteger():
res += l.getInteger() * depth
else:
res += depthSumHelper(l.getList(), depth + 1)
return res
return depthSumHelper(nestedList, 1)