|
42 | 42 | <li>The maximum <strong>depth</strong> of any integer is less than or equal to <code>50</code>.</li>
|
43 | 43 | </ul>
|
44 | 44 |
|
45 |
| - |
46 | 45 | ## Solutions
|
47 | 46 |
|
48 | 47 | <!-- tabs:start -->
|
49 | 48 |
|
50 | 49 | ### **Python3**
|
51 | 50 |
|
52 | 51 | ```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) |
54 | 105 | ```
|
55 | 106 |
|
56 | 107 | ### **Java**
|
57 | 108 |
|
58 | 109 | ```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 | +``` |
59 | 156 |
|
| 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 | +}; |
60 | 216 | ```
|
61 | 217 |
|
62 | 218 | ### **...**
|
|
0 commit comments