File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ import Foundation
22
22
* public func getList() -> [NestedInteger]
23
23
* }
24
24
*/
25
+
26
+ // Old solution
25
27
class Solution {
26
28
func depthSum( _ nestedList: [ NestedInteger ] ) -> Int {
27
29
var wrightSum = 0
@@ -41,3 +43,24 @@ class Solution {
41
43
}
42
44
}
43
45
}
46
+
47
+
48
+
49
+ // New solution
50
+ class Solution {
51
+ func depthSum( _ nestedList: [ NestedInteger ] ) -> Int {
52
+ return depthSumDFSHelper ( nestedList, 1 )
53
+ }
54
+
55
+ func depthSumDFSHelper( _ nestedList: [ NestedInteger ] , _ currentDepth: Int ) -> Int {
56
+ var currentSum = 0
57
+ for nInt in nestedList {
58
+ if nInt. isInteger ( ) {
59
+ currentSum += ( nInt. getInteger ( ) * currentDepth)
60
+ } else {
61
+ currentSum += ( depthSumDFSHelper ( nInt. getList ( ) , currentDepth + 1 ) )
62
+ }
63
+ }
64
+ return currentSum
65
+ }
66
+ }
You can’t perform that action at this time.
0 commit comments