8000 339 · chaor/LeetCode_Python_Accepted@89c1f33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89c1f33

Browse files
committed
339
1 parent 5262447 commit 89c1f33

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

339_Nested_List_Weight_Sum.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 2016-04-02 27 tests, 48 ms
2+
# """
3+
# Thi EF4E s is the interface that allows for creating nested lists.
4+
# You should not implement it, or speculate about its implementation
5+
# """
6+
#class NestedInteger(object):
7+
# def isInteger(self):
8+
# """
9+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
10+
# :rtype bool
11+
# """
12+
#
13+
# def getInteger(self):
14+
# """
15+
# @return the single integer that this NestedInteger holds, if it holds a single integer
16+
# Return None if this NestedInteger holds a nested list
17+
# :rtype int
18+
# """
19+
#
20+
# def getList(self):
21+
# """
22+
# @return the nested list that this NestedInteger holds, if it holds a nested list
23+
# Return None if this NestedInteger holds a single integer
24+
# :rtype List[NestedInteger]
25+
# """
26+
27+
class Solution(object):
28+
def depthSum(self, nestedList):
29+
"""
30+
:type nestedList: List[NestedInteger]
31+
:rtype: int
32+
"""
33+
def scanList(curr_list, depth):
34+
return sum(depth * x.getInteger() if x.isInteger() else scanList(x.getList(), depth + 1) for x in curr_list)
35+
36+
return scanList(nestedList, 1)

0 commit comments

Comments
 (0)
0