Skip to content

Commit 6db9f51

Browse files
committed
feat: add solutions to leetcode problem: No.0339. Nested List Weight Sum
1 parent 7a05fa1 commit 6db9f51

File tree

8 files changed

+569
-6
lines changed

8 files changed

+569
-6
lines changed

solution/0300-0399/0339.Nested List Weight Sum/README.md

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,185 @@
4747
<li>任何整数的最大 <strong>深度</strong> 都小于或等于 <code>50</code></li>
4848
</ul>
4949

50-
5150
## 解法
5251

5352
<!-- 这里可写通用的实现逻辑 -->
5453

54+
DFS 实现。
55+
5556
<!-- tabs:start -->
5657

5758
### **Python3**
5859

5960
<!-- 这里可写当前语言的特殊实现逻辑 -->
6061

6162
```python
62-
63+
# """
64+
# This is the interface that allows for creating nested lists.
65+
# You should not implement it, or speculate about its implementation
66+
# """
67+
#class NestedInteger:
68+
# def __init__(self, value=None):
69+
# """
70+
# If value is not specified, initializes an empty list.
71+
# Otherwise initializes a single integer equal to value.
72+
# """
73+
#
74+
# def isInteger(self):
75+
# """
76+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
77+
# :rtype bool
78+
# """
79+
#
80+
# def add(self, elem):
81+
# """
82+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
83+
# :rtype void
84+
# """
85+
#
86+
# def setInteger(self, value):
87+
# """
88+
# Set this NestedInteger to hold a single integer equal to value.
89+
# :rtype void
90+
# """
91+
#
92+
# def getInteger(self):
93+
# """
94+
# @return the single integer that this NestedInteger holds, if it holds a single integer
95+
# Return None if this NestedInteger holds a nested list
96+
# :rtype int
97+
# """
98+
#
99+
# def getList(self):
100+
# """
101+
# @return the nested list that this NestedInteger holds, if it holds a nested list
102+
# Return None if this NestedInteger holds a single integer
103+
# :rtype List[NestedInteger]
104+
# """
105+
class Solution:
106+
def depthSum(self, nestedList: List[NestedInteger]) -> int:
107+
def dfs(nestedList, depth):
108+
depth_sum = 0
109+
for item in nestedList:
110+
if item.isInteger():
111+
depth_sum += item.getInteger() * depth
112+
else:
113+
depth_sum += dfs(item.getList(), depth + 1)
114+
return depth_sum
115+
return dfs(nestedList, 1)
63116
```
64117

65118
### **Java**
66119

67120
<!-- 这里可写当前语言的特殊实现逻辑 -->
68121

69122
```java
123+
/**
124+
* // This is the interface that allows for creating nested lists.
125+
* // You should not implement it, or speculate about its implementation
126+
* public interface NestedInteger {
127+
* // Constructor initializes an empty nested list.
128+
* public NestedInteger();
129+
*
130+
* // Constructor initializes a single integer.
131+
* public NestedInteger(int value);
132+
*
133+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
134+
* public boolean isInteger();
135+
*
136+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
137+
* // Return null if this NestedInteger holds a nested list
138+
* public Integer getInteger();
139+
*
140+
* // Set this NestedInteger to hold a single integer.
141+
* public void setInteger(int value);
142+
*
143+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
144+
* public void add(NestedInteger ni);
145+
*
146+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
147+
* // Return empty list if this NestedInteger holds a single integer
148+
* public List<NestedInteger> getList();
149+
* }
150+
*/
151+
class Solution {
152+
public int depthSum(List<NestedInteger> nestedList) {
153+
return dfs(nestedList, 1);
154+
}
155+
156+
private int dfs(List<NestedInteger> nestedList, int depth) {
157+
int depthSum = 0;
158+
for (NestedInteger item : nestedList) {
159+
if (item.isInteger()) {
160+
depthSum += item.getInteger() * depth;
161+
} else {
162+
depthSum += dfs(item.getList(), depth + 1);
163+
}
164+
}
165+
return depthSum;
166+
}
167+
}
168+
```
70169

170+
### **JavaScript**
171+
172+
```js
173+
/**
174+
* // This is the interface that allows for creating nested lists.
175+
* // You should not implement it, or speculate about its implementation
176+
* function NestedInteger() {
177+
*
178+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
179+
* @return {boolean}
180+
* this.isInteger = function() {
181+
* ...
182+
* };
183+
*
184+
* Return the single integer that this NestedInteger holds, if it holds a single integer
185+
* Return null if this NestedInteger holds a nested list
186+
* @return {integer}
187+
* this.getInteger = function() {
188+
* ...
189+
* };
190+
*
191+
* Set this NestedInteger to hold a single integer equal to value.
192+
* @return {void}
193+
* this.setInteger = function(value) {
194+
* ...
195+
* };
196+
*
197+
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
198+
* @return {void}
199+
* this.add = function(elem) {
200+
* ...
201+
* };
202+
*
203+
* Return the nested list that this NestedInteger holds, if it holds a nested list
204+
* Return null if this NestedInteger holds a single integer
205+
* @return {NestedInteger[]}
206+
* this.getList = function() {
207+
* ...
208+
* };
209+
* };
210+
*/
211+
/**
212+
* @param {NestedInteger[]} nestedList
213+
* @return {number}
214+
*/
215+
var depthSum = function (nestedList) {
216+
const dfs = (nestedList, depth) => {
217+
let depthSum = 0;
218+
for (const item of nestedList) {
219+
if (item.isInteger()) {
220+
depthSum += item.getInteger() * depth;
221+
} else {
222+
depthSum += dfs(item.getList(), depth + 1);
223+
}
224+
}
225+
return depthSum;
226+
};
227+
return dfs(nestedList, 1);
228+
};
71229
```
72230

73231
### **...**

solution/0300-0399/0339.Nested List Weight Sum/README_EN.md

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,177 @@
4242
<li>The maximum <strong>depth</strong> of any integer is less than or equal to <code>50</code>.</li>
4343
</ul>
4444

45-
4645
## Solutions
4746

4847
<!-- tabs:start -->
4948

5049
### **Python3**
5150

5251
```python
53-
52+
# """
53+
# This is the interface that allows for creating nested lists.
54+
# You should not implement it, or speculate about its implementation
55+
# """
56+
#class NestedInteger:
57+
# def __init__(self, value=None):
58+
# """
59+
# If value is not specified, initializes an empty list.
60+
# Otherwise initializes a single integer equal to value.
61+
# """
62+
#
63+
# def isInteger(self):
64+
# """
65+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
66+
# :rtype bool
67+
# """
68+
#
69+
# def add(self, elem):
70+
# """
71+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
72+
# :rtype void
73+
# """
74+
#
75+
# def setInteger(self, value):
76+
# """
77+
# Set this NestedInteger to hold a single integer equal to value.
78+
# :rtype void
79+
# """
80+
#
81+
# def getInteger(self):
82+
# """
83+
# @return the single integer that this NestedInteger holds, if it holds a single integer
84+
# Return None if this NestedInteger holds a nested list
85+
# :rtype int
86+
# """
87+
#
88+
# def getList(self):
89+
# """
90+
# @return the nested list that this NestedInteger holds, if it holds a nested list
91+
# Return None if this NestedInteger holds a single integer
92+
# :rtype List[NestedInteger]
93+
# """
94+
class Solution:
95+
def depthSum(self, nestedList: List[NestedInteger]) -> int:
96+
def dfs(nestedList, depth):
97+
depth_sum = 0
98+
for item in nestedList:
99+
if item.isInteger():
100+
depth_sum += item.getInteger() * depth
101+
else:
102+
depth_sum += dfs(item.getList(), depth + 1)
103+
return depth_sum
104+
return dfs(nestedList, 1)
54105
```
55106

56107
### **Java**
57108

58109
```java
110+
/**
111+
* // This is the interface that allows for creating nested lists.
112+
* // You should not implement it, or speculate about its implementation
113+
* public interface NestedInteger {
114+
* // Constructor initializes an empty nested list.
115+
* public NestedInteger();
116+
*
117+
* // Constructor initializes a single integer.
118+
* public NestedInteger(int value);
119+
*
120+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
121+
* public boolean isInteger();
122+
*
123+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
124+
* // Return null if this NestedInteger holds a nested list
125+
* public Integer getInteger();
126+
*
127+
* // Set this NestedInteger to hold a single integer.
128+
* public void setInteger(int value);
129+
*
130+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
131+
* public void add(NestedInteger ni);
132+
*
133+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
134+
* // Return empty list if this NestedInteger holds a single integer
135+
* public List<NestedInteger> getList();
136+
* }
137+
*/
138+
class Solution {
139+
public int depthSum(List<NestedInteger> nestedList) {
140+
return dfs(nestedList, 1);
141+
}
142+
143+
private int dfs(List<NestedInteger> nestedList, int depth) {
144+
int depthSum = 0;
145+
for (NestedInteger item : nestedList) {
146+
if (item.isInteger()) {
147+
depthSum += item.getInteger() * depth;
148+
} else {
149+
depthSum += dfs(item.getList(), depth + 1);
150+
}
151+
}
152+
return depthSum;
153+
}
154+
}
155+
```
59156

157+
### **JavaScript**
158+
159+
```js
160+
/**
161+
* // This is the interface that allows for creating nested lists.
162+
* // You should not implement it, or speculate about its implementation
163+
* function NestedInteger() {
164+
*
165+
* Return true if this NestedInteger holds a single integer, rather than a nested list.
166+
* @return {boolean}
167+
* this.isInteger = function() {
168+
* ...
169+
* };
170+
*
171+
* Return the single integer that this NestedInteger holds, if it holds a single integer
172+
* Return null if this NestedInteger holds a nested list
173+
* @return {integer}
174+
* this.getInteger = function() {
175+
* ...
176+
* };
177+
*
178+
* Set this NestedInteger to hold a single integer equal to value.
179+
* @return {void}
180+
* this.setInteger = function(value) {
181+
* ...
182+
* };
183+
*
184+
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
185+
* @return {void}
186+
* this.add = function(elem) {
187+
* ...
188+
* };
189+
*
190+
* Return the nested list that this NestedInteger holds, if it holds a nested list
191+
* Return null if this NestedInteger holds a single integer
192+
* @return {NestedInteger[]}
193+
* this.getList = function() {
194+
* ...
195+
* };
196+
* };
197+
*/
198+
/**
199+
* @param {NestedInteger[]} nestedList
200+
* @return {number}
201+
*/
202+
var depthSum = function (nestedList) {
203+
const dfs = (nestedList, depth) => {
204+
let depthSum = 0;
205+
for (const item of nestedList) {
206+
if (item.isInteger()) {
207+
depthSum += item.getInteger() * depth;
208+
} else {
209+
depthSum += dfs(item.getList(), depth + 1);
210+
}
211+
}
212+
return depthSum;
213+
};
214+
return dfs(nestedList, 1);
215+
};
60216
```
61217

62218
### **...**

0 commit comments

Comments
 (0)