Skip to content

Commit 7278c53

Browse files
author
kwu130
committed
feat(src): add 385-deserialize
1 parent 85fd673 commit 7278c53

1 file changed

Lines changed: 236 additions & 0 deletions

File tree

‎src/385-deserialize.cpp‎

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
//
2+
// Created by 1412 on 2026/4/11.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class NestedInteger {
10+
private:
11+
bool is_int;
12+
int integer_value;
13+
vector<NestedInteger> list_value;
14+
15+
public:
16+
// Constructor initializes an empty nested list.
17+
NestedInteger()
18+
: is_int(false),
19+
integer_value(0) {}
20+
21+
// Constructor initializes a single integer.
22+
explicit NestedInteger(int value)
23+
: is_int(true),
24+
integer_value(value) {}
25+
26+
// Return true if this NestedInteger holds a single integer, rather than a nested list.
27+
bool isInteger() const { return is_int; }
28+
29+
// Return the single integer that this NestedInteger holds, if it holds a single integer
30+
// The result is undefined if this NestedInteger holds a nested list
31+
int getInteger() const { return integer_value; }
32+
33+
// Set this NestedInteger to hold a single integer.
34+
void setInteger(int value) {
35+
is_int = true;
36+
integer_value = value;
37+
list_value.clear();
38+
}
39+
40+
// Set this NestedInteger to hold a nested list and adds a nested integer to it.
41+
void add(const NestedInteger& ni) {
42+
is_int = false;
43+
list_value.push_back(ni);
44+
}
45+
46+
// Return the nested list that this NestedInteger holds, if it holds a nested list
47+
// The result is undefined if this NestedInteger holds a single integer
48+
const vector<NestedInteger>& getList() const { return list_value; }
49+
};
50+
51+
class Solution {
52+
public:
53+
NestedInteger deserialize(string s) {
54+
if (s[0] != '[') {
55+
return NestedInteger(stoi(s));
56+
}
57+
stack<NestedInteger> st;
58+
size_t start = 0; // the start index of a number
59+
for (size_t i = 0; i < s.size(); ++i) {
60+
switch (s[i]) {
61+
case '[':
62+
st.emplace(NestedInteger());
63+
start = i + 1;
64+
break;
65+
case ',':
66+
if (i > start) {
67+
const int num = stoi(s.substr(start, i - start));
68+
st.top().add(NestedInteger(num));
69+
}
70+
start = i + 1;
71+
break;
72+
case ']':
73+
NestedInteger topped = st.top();
74+
st.pop();
75+
if (i > start) {
76+
const int num = stoi(s.substr(start, i - start));
77+
topped.add(NestedInteger(num));
78+
}
79+
if (!st.empty()) {
80+
st.top().add(topped);
81+
} else {
82+
return topped;
83+
}
84+
start = i + 1;
85+
break;
86+
}
87+
}
88+
throw;
89+
}
90+
};
91+
92+
// Helper function to print NestedInteger
93+
void printNestedInteger(const NestedInteger& ni, int indent = 0) {
94+
if (ni.isInteger()) {
95+
cout << string(indent * 2, ' ') << "Integer: " << ni.getInteger() << endl;
96+
} else {
97+
cout << string(indent * 2, ' ') << "List: [" << endl;
98+
for (const auto& item : ni.getList()) {
99+
printNestedInteger(item, indent + 1);
100+
}
101+
cout << string(indent * 2, ' ') << "]" << endl;
102+
}
103+
}
104+
105+
// Helper function to compare two NestedIntegers
106+
bool equalNestedInteger(const NestedInteger& a, const NestedInteger& b) {
107+
if (a.isInteger() != b.isInteger()) {
108+
return false;
109+
}
110+
111+
if (a.isInteger()) {
112+
return a.getInteger() == b.getInteger();
113+
}
114+
115+
const auto& listA = a.getList();
116+
const auto& listB = b.getList();
117+
118+
if (listA.size() != listB.size()) {
119+
return false;
120+
}
121+
122+
for (size_t i = 0; i < listA.size(); ++i) {
123+
if (!equalNestedInteger(listA[i], listB[i])) {
124+
return false;
125+
}
126+
}
127+
128+
return true;
129+
}
130+
131+
void test() {
132+
Solution s;
133+
134+
// Test case 1: Simple integer
135+
{
136+
string input = "324";
137+
NestedInteger result = s.deserialize(input);
138+
assert(result.isInteger());
139+
assert(result.getInteger() == 324);
140+
cout << "Test 1 passed: " << input << endl;
141+
}
142+
143+
// Test case 2: Simple list [1,2,3]
144+
{
145+
string input = "[1,2,3]";
146+
NestedInteger result = s.deserialize(input);
147+
assert(!result.isInteger());
148+
const auto& list = result.getList();
149+
assert(list.size() == 3);
150+
assert(list[0].isInteger() && list[0].getInteger() == 1);
151+
assert(list[1].isInteger() && list[1].getInteger() == 2);
152+
assert(list[2].isInteger() && list[2].getInteger() == 3);
153+
cout << "Test 2 passed: " << input << endl;
154+
}
155+
156+
// Test case 3: Nested list [1,[4,[6]]]
157+
{
158+
string input = "[1,[4,[6]]]";
159+
NestedInteger result = s.deserialize(input);
160+
assert(!result.isInteger());
161+
const auto& list = result.getList();
162+
assert(list.size() == 2);
163+
assert(list[0].isInteger() && list[0].getInteger() == 1);
164+
165+
// Check nested structure: [4,[6]] has 2 elements
166+
assert(!list[1].isInteger());
167+
const auto& nested1 = list[1].getList();
168+
assert(nested1.size() == 2);
169+
assert(nested1[0].isInteger() && nested1[0].getInteger() == 4);
170+
171+
// Check deeply nested: [6]
172+
assert(!nested1[1].isInteger());
173+
const auto& nested2 = nested1[1].getList();
174+
assert(nested2.size() == 1);
175+
assert(nested2[0].isInteger() && nested2[0].getInteger() == 6);
176+
177+
cout << "Test 3 passed: " << input << endl;
178+
}
179+
180+
// Test case 4: Complex nested list [[1,1],2,[1,1]]
181+
{
182+
string input = "[[1,1],2,[1,1]]";
183+
NestedInteger result = s.deserialize(input);
184+
assert(!result.isInteger());
185+
const auto& list = result.getList();
186+
assert(list.size() == 3);
187+
188+
// First element: [1,1]
189+
assert(!list[0].isInteger());
190+
const auto& first = list[0].getList();
191+
assert(first.size() == 2);
192+
assert(first[0].getInteger() == 1);
193+
assert(first[1].getInteger() == 1);
194+
195+
// Second element: 2
196+
assert(list[1].isInteger() && list[1].getInteger() == 2);
197+
198+
// Third element: [1,1]
199+
assert(!list[2].isInteger());
200+
const auto& third = list[2].getList();
201+
assert(third.size() == 2);
202+
assert(third[0].getInteger() == 1);
203+
assert(third[1].getInteger() == 1);
204+
205+
cout << "Test 4 passed: " << input << endl;
206+
}
207+
208+
// Test case 5: Empty list []
209+
{
210+
string input = "[]";
211+
NestedInteger result = s.deserialize(input);
212+
assert(!result.isInteger());
213+
assert(result.getList().empty());
214+
cout << "Test 5 passed: " << input << endl;
215+
}
216+
217+
// Test case 6: Negative numbers [-1,-2,-3]
218+
{
219+
string input = "[-1,-2,-3]";
220+
NestedInteger result = s.deserialize(input);
221+
assert(!result.isInteger());
222+
const auto& list = result.getList();
223+
assert(list.size() == 3);
224+
assert(list[0].getInteger() == -1);
225+
assert(list[1].getInteger() == -2);
226+
assert(list[2].getInteger() == -3);
227+
cout << "Test 6 passed: " << input << endl;
228+
}
229+
230+
cout << "All tests passed!" << endl;
231+
}
232+
233+
int main() {
234+
test();
235+
return 0;
236+
}

0 commit comments

Comments
 (0)