-
Notifications
You must be signed in to change notification settings - Fork 1
/
NestedInteger.h
136 lines (114 loc) · 3.28 KB
/
NestedInteger.h
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
126
127
128
129
130
131
132
133
134
135
136
#ifndef NESTEDINTEGER
#define NESTEDINTEGER
#include <vector>
#include "NormalData.h"
using namespace std;
//class NestedInteger {
//public:
//// Return true if this NestedInteger holds a single integer, rather than a nested list.
//bool isInteger() const { return l.empty(); };
//// 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 getInteger() const {
//if (isInteger()) {
//return num;
//}
//else {
//return 0;
//}
//};
//// 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
//const vector<NestedInteger> &getList() const {
//if (!l.empty()) {
//return l;
//}
//else {
//return l;
//}
//};
//NestedInteger(const vector<NestedInteger> &a): l(a) {}
//NestedInteger(int a): num(a) { }
//private:
//int num;
//vector<NestedInteger> l;
//};
class NestedInteger {
public:
// Constructor initializes an empty nested list.
NestedInteger() {}
// Constructor initializes a single integer.
NestedInteger(int value): num(value) {}
NestedInteger(const vector<NestedInteger> &a): l(a) {}
// Return true if this NestedInteger holds a single integer, rather than a nested list.
bool isInteger() const { return l.empty(); }
// 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 getInteger() const {
if (isInteger()) {
return num;
}
else {
return 0;
}
}
// Set this NestedInteger to hold a single integer.
void setInteger(int value) {
l.clear();
num = value;
}
// Set this NestedInteger to hold a nested list and adds a nested integer to it.
void add(const NestedInteger &ni) {
num = 0;
l.push_back(ni);
}
// 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
const vector<NestedInteger> &getList() const {
if (!l.empty()) {
return l;
}
else {
return l;
}
}
private:
int num;
vector<NestedInteger> l;
};
void printNestedInteger(NestedInteger& a) {
if (a.isInteger()) {
cout << a.getInteger();
}
else {
cout << "[";
bool firstElement = true;
vector<NestedInteger> l = a.getList();
for (auto i: l) {
if (firstElement) {
firstElement = false;
}
else {
cout << ", ";
}
printNestedInteger(i);
}
cout << "]";
}
}
template<>
void printVector<NestedInteger>(const vector<NestedInteger> &a) {
cout << "[";
bool firstElement = true;
for (auto i: a) {
if (firstElement) {
firstElement = false;
}
else {
cout << ", ";
}
printNestedInteger(i);
}
cout << "]" << endl;
}
#endif