-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0339-NestedListWeightSum.cs
125 lines (109 loc) · 3.91 KB
/
0339-NestedListWeightSum.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//-----------------------------------------------------------------------------
// Runtime: 60ms
// Memory Usage: 19.5 MB
// Link: https://leetcode.com/submissions/detail/328300270/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* interface NestedInteger {
*
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* bool IsInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* int GetInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void SetInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void Add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* IList<NestedInteger> GetList();
* }
*/
public class _0339_NestedListWeightSum
{
public int DepthSum(IList<NestedInteger> nestedList)
{
var sum = 0;
foreach (var item in nestedList)
{
sum += SumNestedInteger(item, 1);
}
return sum;
}
private int SumNestedInteger(NestedInteger ni, int depth)
{
if (ni.IsInteger())
return ni.GetInteger().Value * depth;
else
{
var sum = 0;
foreach (var item in ni.GetList())
sum += SumNestedInteger(item, depth + 1);
return sum;
}
}
public class NestedInteger
{
private int? number;
private IList<NestedInteger> list;
// Constructor initializes an empty nested list.
public NestedInteger()
{
number = null;
}
// Constructor initializes a single integer.
public NestedInteger(int value)
{
number = value;
}
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public bool IsInteger()
{
return number.HasValue;
}
// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
public int? GetInteger()
{
return number;
}
// Set this NestedInteger to hold a single integer.
public void SetInteger(int value)
{
number = value;
list = null;
}
// Set this NestedInteger to hold a nested list and adds a nested integer to it.
public void Add(NestedInteger ni)
{
number = null;
if (list == null)
list = new List<NestedInteger>() { ni };
else
list.Add(ni);
}
// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
public IList<NestedInteger> GetList()
{
return list;
}
}
}
}