|
| 1 | +<h2><a href="https://leetcode.com/problems/nested-list-weight-sum">339. Nested List Weight Sum</a></h2><h3>Medium</h3><hr><p>You are given a nested list of integers <code>nestedList</code>. Each element is either an integer or a list whose elements may also be integers or other lists.</p> |
| 2 | + |
| 3 | +<p>The <strong>depth</strong> of an integer is the number of lists that it is inside of. For example, the nested list <code>[1,[2,2],[[3],2],1]</code> has each integer's value set to its <strong>depth</strong>.</p> |
| 4 | + |
| 5 | +<p>Return <em>the sum of each integer in </em><code>nestedList</code><em> multiplied by its <strong>depth</strong></em>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/nestedlistweightsumex1.png" style="width: 405px; height: 99px;" /> |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> nestedList = [[1,1],2,[1,1]] |
| 12 | +<strong>Output:</strong> 10 |
| 13 | +<strong>Explanation:</strong> Four 1's at depth 2, one 2 at depth 1. 1*2 + 1*2 + 2*1 + 1*2 + 1*2 = 10. |
| 14 | +</pre> |
| 15 | + |
| 16 | +<p><strong class="example">Example 2:</strong></p> |
| 17 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/nestedlistweightsumex2.png" style="width: 315px; height: 106px;" /> |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> nestedList = [1,[4,[6]]] |
| 20 | +<strong>Output:</strong> 27 |
| 21 | +<strong>Explanation:</strong> One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3. 1*1 + 4*2 + 6*3 = 27.</pre> |
| 22 | + |
| 23 | +<p><strong class="example">Example 3:</strong></p> |
| 24 | + |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> nestedList = [0] |
| 27 | +<strong>Output:</strong> 0 |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>1 <= nestedList.length <= 50</code></li> |
| 35 | + <li>The values of the integers in the nested list is in the range <code>[-100, 100]</code>.</li> |
| 36 | + <li>The maximum <strong>depth</strong> of any integer is less than or equal to <code>50</code>.</li> |
| 37 | +</ul> |
0 commit comments