Skip to content

Commit c2e0141

Browse files
committedApr 15, 2022
feat: add solutions to lc problem: No.0385
No.0385.Mini Parser
1 parent 30e163c commit c2e0141

File tree

5 files changed

+503
-2
lines changed

5 files changed

+503
-2
lines changed
 

‎solution/0300-0399/0385.Mini Parser/README.md

+169-1
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,183 @@
5555
<!-- 这里可写当前语言的特殊实现逻辑 -->
5656

5757
```python
58-
58+
# """
59+
# This is the interface that allows for creating nested lists.
60+
# You should not implement it, or speculate about its implementation
61+
# """
62+
# class NestedInteger:
63+
# def __init__(self, value=None):
64+
# """
65+
# If value is not specified, initializes an empty list.
66+
# Otherwise initializes a single integer equal to value.
67+
# """
68+
#
69+
# def isInteger(self):
70+
# """
71+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
72+
# :rtype bool
73+
# """
74+
#
75+
# def add(self, elem):
76+
# """
77+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
78+
# :rtype void
79+
# """
80+
#
81+
# def setInteger(self, value):
82+
# """
83+
# Set this NestedInteger to hold a single integer equal to value.
84+
# :rtype void
85+
# """
86+
#
87+
# def getInteger(self):
88+
# """
89+
# @return the single integer that this NestedInteger holds, if it holds a single integer
90+
# Return None if this NestedInteger holds a nested list
91+
# :rtype int
92+
# """
93+
#
94+
# def getList(self):
95+
# """
96+
# @return the nested list that this NestedInteger holds, if it holds a nested list
97+
# Return None if this NestedInteger holds a single integer
98+
# :rtype List[NestedInteger]
99+
# """
100+
class Solution:
101+
def deserialize(self, s: str) -> NestedInteger:
102+
if not s:
103+
return NestedInteger()
104+
if s[0] != '[':
105+
return NestedInteger(int(s))
106+
if len(s) <= 2:
107+
return NestedInteger()
108+
ans = NestedInteger()
109+
depth, j = 0, 1
110+
for i in range(1, len(s)):
111+
if depth == 0 and (s[i] == ',' or i == len(s) - 1):
112+
ans.add(self.deserialize(s[j:i]))
113+
j = i + 1
114+
elif s[i] == '[':
115+
depth += 1
116+
elif s[i] == ']':
117+
depth -= 1
118+
return ans
59119
```
60120

61121
### **Java**
62122

63123
<!-- 这里可写当前语言的特殊实现逻辑 -->
64124

65125
```java
126+
/**
127+
* // This is the interface that allows for creating nested lists.
128+
* // You should not implement it, or speculate about its implementation
129+
* public interface NestedInteger {
130+
* // Constructor initializes an empty nested list.
131+
* public NestedInteger();
132+
*
133+
* // Constructor initializes a single integer.
134+
* public NestedInteger(int value);
135+
*
136+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
137+
* public boolean isInteger();
138+
*
139+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
140+
* // Return null if this NestedInteger holds a nested list
141+
* public Integer getInteger();
142+
*
143+
* // Set this NestedInteger to hold a single integer.
144+
* public void setInteger(int value);
145+
*
146+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
147+
* public void add(NestedInteger ni);
148+
*
149+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
150+
* // Return empty list if this NestedInteger holds a single integer
151+
* public List<NestedInteger> getList();
152+
* }
153+
*/
154+
class Solution {
155+
public NestedInteger deserialize(String s) {
156+
if ("".equals(s)) {
157+
return new NestedInteger();
158+
}
159+
if (s.charAt(0) != '[') {
160+
return new NestedInteger(Integer.parseInt(s));
161+
}
162+
if (s.length() <= 2) {
163+
return new NestedInteger();
164+
}
165+
NestedInteger ans = new NestedInteger();
166+
int depth = 0;
167+
for (int i = 1, j = 1; i < s.length(); ++i) {
168+
if (depth == 0 && (s.charAt(i) == ',' || i == s.length() - 1)) {
169+
ans.add(deserialize(s.substring(j, i)));
170+
j = i + 1;
171+
} else if (s.charAt(i) == '[') {
172+
++depth;
173+
} else if (s.charAt(i) == ']') {
174+
--depth;
175+
}
176+
}
177+
return ans;
178+
}
179+
}
180+
```
66181

182+
### **C++**
183+
184+
```cpp
185+
/**
186+
* // This is the interface that allows for creating nested lists.
187+
* // You should not implement it, or speculate about its implementation
188+
* class NestedInteger {
189+
* public:
190+
* // Constructor initializes an empty nested list.
191+
* NestedInteger();
192+
*
193+
* // Constructor initializes a single integer.
194+
* NestedInteger(int value);
195+
*
196+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
197+
* bool isInteger() const;
198+
*
199+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
200+
* // The result is undefined if this NestedInteger holds a nested list
201+
* int getInteger() const;
202+
*
203+
* // Set this NestedInteger to hold a single integer.
204+
* void setInteger(int value);
205+
*
206+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
207+
* void add(const NestedInteger &ni);
208+
*
209+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
210+
* // The result is undefined if this NestedInteger holds a single integer
211+
* const vector<NestedInteger> &getList() const;
212+
* };
213+
*/
214+
class Solution {
215+
public:
216+
NestedInteger deserialize(string s) {
217+
if (s.empty()) return NestedInteger();
218+
if (s[0] != '[') return NestedInteger(stoi(s));
219+
if (s.size() <= 2) return NestedInteger();
220+
NestedInteger ans;
221+
int depth = 0;
222+
for (int i = 1, j = 1; i < s.size(); ++i)
223+
{
224+
if (depth == 0 && (s[i] == ',' || i == s.size() - 1))
225+
{
226+
ans.add(deserialize(s.substr(j, i - j)));
227+
j = i + 1;
228+
}
229+
else if (s[i] == '[') ++depth;
230+
else if (s[i] == ']') --depth;
231+
}
232+
return ans;
233+
}
234+
};
67235
```
68236
69237
### **...**

‎solution/0300-0399/0385.Mini Parser/README_EN.md

+169-1
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,181 @@
4747
### **Python3**
4848

4949
```python
50-
50+
# """
51+
# This is the interface that allows for creating nested lists.
52+
# You should not implement it, or speculate about its implementation
53+
# """
54+
# class NestedInteger:
55+
# def __init__(self, value=None):
56+
# """
57+
# If value is not specified, initializes an empty list.
58+
# Otherwise initializes a single integer equal to value.
59+
# """
60+
#
61+
# def isInteger(self):
62+
# """
63+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
64+
# :rtype bool
65+
# """
66+
#
67+
# def add(self, elem):
68+
# """
69+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
70+
# :rtype void
71+
# """
72+
#
73+
# def setInteger(self, value):
74+
# """
75+
# Set this NestedInteger to hold a single integer equal to value.
76+
# :rtype void
77+
# """
78+
#
79+
# def getInteger(self):
80+
# """
81+
# @return the single integer that this NestedInteger holds, if it holds a single integer
82+
# Return None if this NestedInteger holds a nested list
83+
# :rtype int
84+
# """
85+
#
86+
# def getList(self):
87+
# """
88+
# @return the nested list that this NestedInteger holds, if it holds a nested list
89+
# Return None if this NestedInteger holds a single integer
90+
# :rtype List[NestedInteger]
91+
# """
92+
class Solution:
93+
def deserialize(self, s: str) -> NestedInteger:
94+
if not s:
95+
return NestedInteger()
96+
if s[0] != '[':
97+
return NestedInteger(int(s))
98+
if len(s) <= 2:
99+
return NestedInteger()
100+
ans = NestedInteger()
101+
depth, j = 0, 1
102+
for i in range(1, len(s)):
103+
if depth == 0 and (s[i] == ',' or i == len(s) - 1):
104+
ans.add(self.deserialize(s[j:i]))
105+
j = i + 1
106+
elif s[i] == '[':
107+
depth += 1
108+
elif s[i] == ']':
109+
depth -= 1
110+
return ans
51111
```
52112

53113
### **Java**
54114

55115
```java
116+
/**
117+
* // This is the interface that allows for creating nested lists.
118+
* // You should not implement it, or speculate about its implementation
119+
* public interface NestedInteger {
120+
* // Constructor initializes an empty nested list.
121+
* public NestedInteger();
122+
*
123+
* // Constructor initializes a single integer.
124+
* public NestedInteger(int value);
125+
*
126+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
127+
* public boolean isInteger();
128+
*
129+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
130+
* // Return null if this NestedInteger holds a nested list
131+
* public Integer getInteger();
132+
*
133+
* // Set this NestedInteger to hold a single integer.
134+
* public void setInteger(int value);
135+
*
136+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
137+
* public void add(NestedInteger ni);
138+
*
139+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
140+
* // Return empty list if this NestedInteger holds a single integer
141+
* public List<NestedInteger> getList();
142+
* }
143+
*/
144+
class Solution {
145+
public NestedInteger deserialize(String s) {
146+
if ("".equals(s)) {
147+
return new NestedInteger();
148+
}
149+
if (s.charAt(0) != '[') {
150+
return new NestedInteger(Integer.parseInt(s));
151+
}
152+
if (s.length() <= 2) {
153+
return new NestedInteger();
154+
}
155+
NestedInteger ans = new NestedInteger();
156+
int depth = 0;
157+
for (int i = 1, j = 1; i < s.length(); ++i) {
158+
if (depth == 0 && (s.charAt(i) == ',' || i == s.length() - 1)) {
159+
ans.add(deserialize(s.substring(j, i)));
160+
j = i + 1;
161+
} else if (s.charAt(i) == '[') {
162+
++depth;
163+
} else if (s.charAt(i) == ']') {
164+
--depth;
165+
}
166+
}
167+
return ans;
168+
}
169+
}
170+
```
56171

172+
### **C++**
173+
174+
```cpp
175+
/**
176+
* // This is the interface that allows for creating nested lists.
177+
* // You should not implement it, or speculate about its implementation
178+
* class NestedInteger {
179+
* public:
180+
* // Constructor initializes an empty nested list.
181+
* NestedInteger();
182+
*
183+
* // Constructor initializes a single integer.
184+
* NestedInteger(int value);
185+
*
186+
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
187+
* bool isInteger() const;
188+
*
189+
* // Return the single integer that this NestedInteger holds, if it holds a single integer
190+
* // The result is undefined if this NestedInteger holds a nested list
191+
* int getInteger() const;
192+
*
193+
* // Set this NestedInteger to hold a single integer.
194+
* void setInteger(int value);
195+
*
196+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
197+
* void add(const NestedInteger &ni);
198+
*
199+
* // Return the nested list that this NestedInteger holds, if it holds a nested list
200+
* // The result is undefined if this NestedInteger holds a single integer
201+
* const vector<NestedInteger> &getList() const;
202+
* };
203+
*/
204+
class Solution {
205+
public:
206+
NestedInteger deserialize(string s) {
207+
if (s.empty()) return NestedInteger();
208+
if (s[0] != '[') return NestedInteger(stoi(s));
209+
if (s.size() <= 2) return NestedInteger();
210+
NestedInteger ans;
211+
int depth = 0;
212+
for (int i = 1, j = 1; i < s.size(); ++i)
213+
{
214+
if (depth == 0 && (s[i] == ',' || i == s.size() - 1))
215+
{
216+
ans.add(deserialize(s.substr(j, i - j)));
217+
j = i + 1;
218+
}
219+
else if (s[i] == '[') ++depth;
220+
else if (s[i] == ']') --depth;
221+
}
222+
return ans;
223+
}
224+
};
57225
```
58226
59227
### **...**

0 commit comments

Comments
 (0)