// This is the interface that allows for creating nested lists.// You should not implement it, or speculate about its implementationpublicinterfaceNestedInteger {
// Constructor initializes an empty nested list.publicNestedInteger();
// Constructor initializes a single integer.publicNestedInteger(int value);
// @return true if this NestedInteger holds a single integer, rather than a// nested list.publicbooleanisInteger();
// @return the single integer that this NestedInteger holds, if it holds a// single integer Return null if this NestedInteger holds a nested listpublic Integer getInteger();
// Set this NestedInteger to hold a single integer.publicvoidsetInteger(int value);
// Set this NestedInteger to hold a nested list and adds a nested integer to// it.publicvoidadd(NestedInteger ni);
// @return the nested list that this NestedInteger holds, if it holds a// nested list// Return empty list if this NestedInteger holds a single integerpublic List<NestedInteger>getList();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
publicintdepthSum(List<NestedInteger> nestedList) {
return dfs(nestedList, 1);
}
privateintdfs(List<NestedInteger> nestedList, int depth) {
int total = 0;
for (NestedInteger ni : nestedList) {
if (ni.isInteger()) {
total += ni.getInteger() * depth;
} else {
total += depthSumHelper(ni.getList(), depth + 1);
}
}
return total;
}
}
classNestedInteger:
def __init__(self, value=None):
"""
If value is not specified, initializes an empty list.
Otherwise initializes a single integer equal to value.
"""defisInteger(self):
"""
@return True if this NestedInteger holds a single integer, rather than a nested list.
:rtype bool
"""defadd(self, elem):
"""
Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
:rtype void
"""defsetInteger(self, value):
"""
Set this NestedInteger to hold a single integer equal to value.
:rtype void
"""defgetInteger(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
"""defgetList(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]
"""
publicclassSolution {
publicintdepthSum(List<NestedInteger> nestedList) {
int total = 0;
Stack<Pair<List<NestedInteger>, Integer>> stack =new Stack<>();
stack.push(new Pair<>(nestedList, 1));
while (!stack.isEmpty()) {
Pair<List<NestedInteger>, Integer> current = stack.pop();
List<NestedInteger> currentList = current.getKey();
int depth = current.getValue();
for (NestedInteger ni : currentList) {
if (ni.isInteger()) {
total += ni.getInteger() * depth;
} else {
stack.push(new Pair<>(ni.getList(), depth + 1));
}
}
}
return total;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defdepthSum(self, nestedList: List[NestedInteger]) -> int:
total =0 stack = [
(nestedList, 1)
] # Each element is a tuple (current_list, current_depth)while stack:
current_list, depth = stack.pop()
for item in current_list:
if item.isInteger():
total += item * depth # Add weighted valueelse:
stack.append(
(item, depth +1)
) # Add nested list with increased depthreturn total