当前位置:网站首页>Leetcode notes 339 Nested list weights and
Leetcode notes 339 Nested list weights and
2022-07-21 21:49:00 【Lyrig~】
Leetcode note 339. Nested list weights and
subject
Topic linking
Given a nested list of integers nestedList , Each element is either an integer , Or a list . meanwhile , The elements in the list can also be integers or another list .
The integer depth Is the number of nesting levels within the list . for example , Nested list [1,[2,2],[[3],2],1] The value of each integer in is its depth .
Please return the sum of all integers after the list is weighted by depth .
Example 1:
Input :nestedList = [[1,1],2,[1,1]]
Output :10
explain : Because there are four depths in the list 2 Of 1 , And a depth of 1 Of 2.
Example 2:
Input :nestedList = [1,[4,[6]]]
Output :27
explain : One depth is 1 Of 1, One depth is 2 Of 4, One depth is 3 Of 6. therefore ,1 + 42 + 63 = 27.
Example 3:
Input :nestedList = [0]
Output :0
Tips :
1 <= nestedList.length <= 50
The value of an integer in the nested list is in the range [-100, 100] Inside
The maximum of any integer depth Are less than or equal to 50
Ideas
Linear traversal can be achieved through recursion , So as to solve the problem quickly .
Code
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* public:
* // Constructor initializes an empty nested list.
* NestedInteger();
*
* // Constructor initializes a single integer.
* NestedInteger(int value);
*
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* bool isInteger() const;
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* int getInteger() const;
*
* // Set this NestedInteger to hold a single integer.
* void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* void add(const NestedInteger &ni);
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* const vector<NestedInteger> &getList() const;
* };
*/
class Solution {
public:
int step = 0;
int depthSum(vector<NestedInteger>& nestedList) {
int ans = 0;
++ step;
int n = nestedList.size();
for(int i = 0; i < n; ++ i)
{
if(nestedList[i].isInteger())
{
ans += nestedList[i].getInteger() * step;
}
else{
ans += depthSum(nestedList[i].getList());
}
}
-- step;
return ans;
}
};
What's good here is mainly the realization depthSum Reuse of this function , By introducing external step, Used to guide the current depthSum Calculated layers , Then recover layer by layer , Its final implementation is completed in linear time .
Complexity analysis
Because it's linear , Its time complexity O ( n ) \mathcal{O}(n) O(n)
The spatial complexity may be O ( 1 ) \mathcal{O}(1) O(1)?
边栏推荐
- The growth path of Qualcomm camera software engineers
- Notes on generating models (VI): generating models
- swin-transformer代码详解
- 007_ SSSSS_ Neural Ordinary Differential Equtions
- 生物JC 磷酸化蛋白组分析解释了CDKs在TNF信号通路中的关键作用
- Detailed explanation of SQL Server index Foundation___ Concept and principle
- 【转载】对话动作集定义CUED Standard Dialogue Acts
- 基于EasyCV复现DETR和DAB-DETR,Object Query的正确打开方式
- 音视频开发学习笔记(一)
- Sub view settings layoutparams sliding is not smooth flashing problem
猜你喜欢
One of kubernetes resource arrangement series: pod yaml
[create birthday greetings in kotlin]
Notes on generating models (VI): generating models
Kotlin basic data type
生成模型笔记(五):判别模型
[write your first program with kotlin]
15 in-depth articles that developers can't miss when playing with machine learning
SQL server and azure SQL index architecture and Design Guide
吴恩达机器学习系列课程汇总(视频+部分汉化+讲义+作业)
Transplant grpc to arm board
随机推荐
音视频学习笔记(雷神)—技术解析
Wonderful journey of quantum mechanics - pre preparation
SQL server and azure SQL index architecture and Design Guide
007 Bar _ Sssss Neural Ordinary Differential Equations
Alibaba's super large-scale Flink cluster operation and maintenance practice
Getting started with kotlin
生成模型笔记(六):生成模型
004_SSSS_ Image-to-Image Translation with Conditional Adversarial Networks
基于EasyCV复现ViTDet:单层特征超越FPN
阿里云机器学习平台PAI与华东师范大学论文入选SIGIR 2022
Overview of DTS GIC interrupt controller
Transplant grpc to arm board
【RPG Maker MV】使用技巧1:用自己绘制的图片当做地图
UWB环境配置记录
SereTOD2022数据剖析-面向半监督和强化学习的任务型对话系统挑战赛
那些年用node接入微信走过的坑之(五)---微信菜单(自动回复素材)
How to install single node MySQL with RPM in CentOS 7
Notes on generating models (III): approximate inference
Sreworks continues to deliver cloud prototype: image construction
[SQLite3 database]