forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
77 lines (65 loc) · 1.47 KB
/
main.cpp
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
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class NestedInteger {
private:
int value;
bool is_integer;
vector<NestedInteger> children;
public:
NestedInteger(int value, bool is_integer, vector<NestedInteger> children) {
this->value = value;
this->is_integer = is_integer;
this->children = children;
}
bool isInteger() const {
return is_integer;
}
int getInteger() const {
return value;
}
vector<NestedInteger> &getList() {
return children;
}
};
class Solution {
public:
int depthSum(vector<NestedInteger>& nestedList) {
return depthSumHelper(nestedList, 1);
}
private:
int depthSumHelper(vector<NestedInteger>& nestedList, int depth) {
int ret = 0;
for (auto& list : nestedList) {
if (list.isInteger()) {
ret += list.getInteger() * depth;
} else {
ret += depthSumHelper(list.getList(), depth + 1);
}
}
return ret;
}
};
int main() {
Solution sol;
vector<NestedInteger> empty_vector = {};
NestedInteger one(1, true, empty_vector);
vector<NestedInteger> fragment = {one, one};
vector<NestedInteger> list = {
NestedInteger(0, false, fragment),
NestedInteger(2, true, empty_vector),
NestedInteger(0, false, fragment),
};
cout << sol.depthSum(list);
return 0;
}