[LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Example 1:
Input: [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1's at depth 2, one 2 at depth 1.
Example 2:
Input: [1,[4,[6]]]
Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27.
题目思路为DFS, 用一个helper function去记录nestInteger的值, 传入的input包括depth.
Data Structure.
# """
# 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]
# """
Code
class Solution:
def NestedListWeightSum(self, nestedList):
def helper(nestedList, depth):
s = 0
for each in nestedList:
if each.isInteger():
s += each.getInteger() * depth
else:
s += dfs(each.getList(), depth + 1)
return s
return helper(nestedList, 1)
[LeetCode] 339. Nested List Weight Sum_Easy tag:DFS的更多相关文章
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- LeetCode 339. Nested List Weight Sum (嵌套列表重和)$
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [leetcode]339. Nested List Weight Sum嵌套列表加权和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [leetcode]364. Nested List Weight Sum II嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- 【leetcode】339. Nested List Weight Sum
原题 Given a nested list of integers, return the sum of all integers in the list weighted by their dep ...
- [LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- 【LeetCode】339. Nested List Weight Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...
随机推荐
- C - 取石子游戏
1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍.取完者胜.先取者负输出"Second win".先取者胜输出&q ...
- 2017-2018 ACM-ICPC Latin American Regional Programming Contest
题面pdfhttps://codeforc.es/gym/101889/attachments/download/7471/statements-2017-latam-regional.pdf zyn ...
- ubuntu16.04 ROS环境下配置和运行SVO
ubuntu16.04 ROS环境下配置和运行SVO https://blog.csdn.net/nnUyi/article/details/78005552
- python数据结构之图论
本篇学习笔记内容为图的各项性质.图的表示方法.图ADT的python实现 图(Graph) 是数据结构和算法学中最强大的框架之一(或许没有之一).图几乎可以用来表现所有类型的结构或系统,从交通网络到通 ...
- samba速度调优
本来windows上传到板子上的速度很慢 增加 socket options = TCP_NODELAY 明显上传下载速度都快了 参考: https://superuser.com/questions ...
- MachineLearning:
https://github.com/pennyliang/MachineLearning-C---code https://zhuanlan.zhihu.com/p/22794772 http:// ...
- oracle优化:避免全表扫描
http://blog.csdn.net/onetree2010/article/details/6098259
- 天使玩偶:CDQ分治
这道好(du)题(liu)还是很不错的 挺锻炼代码能力和不断优化 卡常的能力的. 对于 每次询问 我都可以将其分出方向 然后 写 也就是针对于4个方向 左下 左上 右下 右上 这样的话 就成功转换了问 ...
- Python判断字符串是否为字母或者数字
严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...
- day5_判断价格输入是否是正整数或正小数
def check_float_integer(s): #判断价格正确的正整数或正小数 s = str(s) if check_integer(s) == True: return True elif ...