[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 ...
随机推荐
- day_4.30 py
2018-4-30 13:02:32 ''' 多态:只有当调用方法的时候才知道调用父类 还是子类的方法(随变化而变化,等到真正实行的时候才知道结果) 面向对象三个特点: 封装 继承 多态 ''' cl ...
- 一名前端Web架构师的成长之路(转载)
本人也是coding很多年,虽然很失败,但也总算有点失败的心得,不过我在中国,大多数程序员都是像我一样,在一直走着弯路.如果想成为一个架构师,就必须走正确的路,否则离目标越来越远,正在辛苦工作的程序员 ...
- 免费的SSL证书(LINUX)
贫穷限制了我的SSL. 说起来也简单,免费的SSL证书授权机构,我使用的是Certbot 选择服务器开启的服务,像我php之流,无非apache和nginx,然后选择使用的服务器类型.嗯,补充一句,这 ...
- System.InvalidOperationException: 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。
x 昨天还好好地,然后清理一下电脑垃圾,就突然报这个错误了; 网上搜索了一下:找到解决方案了,但是由于底层知识的功力不够,至今未知具体怎么导致的... 解决方案↓ 进注册表 按Win+R运行reged ...
- .NET Core错误:The specified framework 'Microsoft.NETCore.App', version '1.0.0-rc2-3002702' was not found.
本地Dos命令行中,cd到你的项目目录下,生成, dotnet {U_Project_Name}.dll 发布 dotnet publish ,然后将发布的文件夹中的文件全部拷贝到服务器中,至此,问题 ...
- .NET Core开发日志——Middleware
熟悉ASP.NET架构的开发者一定对于HTTP Modules与HTTP Handlers不陌生.两者的作用主要是对网络请求执行特定的处理工作.而在.NET Core中,它们都被Middleware( ...
- Django:管理站点
1.自定义管理页面 自定义管理界面需要创建一个类,继承admin.ModelAdmin booktest/admin.py class BookInfoAdmin(admin.ModelAdmin): ...
- DHCP协议和PXE
在学习IP地址基本概念之后,需要了解到如果需要和其他机器通讯,我们就需要一个通讯地址,我们需要给网卡配置一个地址. 如何配置 IP 地址? 可以用命令行自己配置一个地址.可以使用 ifconfig,也 ...
- hung_task_timeout_secs和blocked for more than 120 seconds的解决方法
Linux系统出现hung_task_timeout_secs和blocked for more than 120 seconds的解决方法 Linux系统出现系统没有响应. 在/var/log/me ...
- Kafka – kafka consumer
ConsumerRecords<String, String> records = consumer.poll(100); /** * Fetch data for the topic ...