[抄题]:

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:
Given the list [[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1)

Example 2:
Given the list [1,[4,[6]]], return 27. (one 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27)

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

对新的数据结构完全没有思路啊

[一句话思路]:

对列表List<随便什么东西>可以调用isInteger()和getInteger()取数,getList()取列表方法

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 忘记乘以depth了,用?:的时候要该做的运算还是要做的,不能忘了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

对列表List<随便什么东西>可以调用isInteger()和getInteger()取数,getList()取列表方法

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

for (NestedInteger e : list) {
ans += e.isInteger() ? e.getInteger() * depth : helper(e.getList(), depth + 1);
}

对于list那么长的NestedInteger 数据类型

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

包络型整数

364. Nested List Weight Sum II 权重相反-想得出来大概意思,写不出来。还是写得太少

690. Employee Importance 同上

[代码风格] :

class Solution {
public int depthSum(List<NestedInteger> nestedList) {
return helper(nestedList, 1);
} public int helper(List<NestedInteger> list, int depth) {
int ans = 0;
for (NestedInteger e : list) {
ans += e.isInteger() ? e.getInteger() * depth : helper(e.getList(), depth + 1);
}
return ans;
}
}

嵌套列表的加权和 · Nested List Weight Sum的更多相关文章

  1. [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. ...

  2. [LeetCode] Nested List Weight Sum II 嵌套链表权重和之二

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  3. [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. ...

  4. 【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 ...

  5. LeetCode Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  6. Nested List Weight Sum I & II

    Nested List Weight Sum I Given a nested list of integers, return the sum of all integers in the list ...

  7. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  8. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  9. [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. ...

随机推荐

  1. 软件设计----LisKov替换原则(LSP)

    LisKov替换原则的定义:一个软件实体如果使用的是一个基类的话,一定适用于其子类,而且根本不能觉察出基类对象和子类对象的区别. 1)怎么理解上面的概念?就是我们程序设计的子类型能够完全替换父类型,而 ...

  2. webdriver常用API

    本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...

  3. JAVASE02-Unit011: TCP通信(小程序)

    TCP通信(小程序) server端: package chat; import java.io.BufferedReader; import java.io.IOException; import ...

  4. PES包格式

    PES是Packetized Elementary Stream的简称,是将原始ES流打包后形成的,再将PES经过不同的打包方式可以组成MPEG program stream 和 MPEG trans ...

  5. Hibernate学习4—关联关系一对多映射

    一.班级学生一对多映射实现(单向) Class 1 * Student 一对多 在学生多的一方,配置一的对象:   com.cy.model.Class: package com.cy.model; ...

  6. pagination实现分页功能

    pagination.js: /** * pagination分页插件 */ ;(function($,window,document,undefined){ //配置参数 var defaults ...

  7. Python模块包(pycharm右键创建文件夹和python package的区别)中__init__.py文件的作用

    在eclipse中用pydev开发Python脚本时,我遇到了一个这样的现象,当我新建一个pydev package时,总会自动地生成一个空的__init__.py文件,因为是python新手,所以很 ...

  8. 学习:Dom4j和Xpath

    1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和JAXP. DOM4J使 ...

  9. Oracle调优之buffer pool相关

    一个oracle block与data buffer中的一个buffer对应.用户进程(server process)负责读取磁盘上的block到data buffer cache中,DEWn进程负责 ...

  10. Apache 的编译安装

    直接看步骤: 1.安装必备环境:gcc.pcre .................................. 2.安装apr.apr-util 3.解压编译安装Apache: ./confi ...