-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm339.c
54 lines (50 loc) · 2.15 KB
/
m339.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* *********************************************************************
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* *********************************************************************
*
* // Initializes an empty nested list and return a reference to the nested integer.
* struct NestedInteger *NestedIntegerInit();
*
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* bool NestedIntegerIsInteger(struct NestedInteger *);
*
* // 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 NestedIntegerGetInteger(struct NestedInteger *);
*
* // Set this NestedInteger to hold a single integer.
* void NestedIntegerSetInteger(struct NestedInteger *ni, int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
* void NestedIntegerAdd(struct NestedInteger *ni, struct NestedInteger *elem);
*
* // 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
* struct NestedInteger **NestedIntegerGetList(struct NestedInteger *);
*
* // Return the nested list's size that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* int NestedIntegerGetListSize(struct NestedInteger *);
* };
*/
int helper(struct NestedInteger* curr, int multiplier) {
if (NestedIntegerIsInteger(curr)) {
return multiplier * NestedIntegerGetInteger(curr);
}
int lstSize = NestedIntegerGetListSize(curr);
struct NestedInteger** lst = NestedIntegerGetList(curr);
int output = 0;
for (int i = 0; i < lstSize; i++) {
output += helper(lst[i], multiplier + 1);
}
return output;
}
int depthSum(struct NestedInteger** nestedList, int nestedListSize) {
int output = 0;
for (int i = 0; i < nestedListSize; i++) {
output += helper(nestedList[i], 1);
}
return output;
}