Skip to content

Commit 46c73ca

Browse files
committed
Mini Parser
1 parent 72c73e5 commit 46c73ca

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

385-mini-parser.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
Problem Link: https://leetcode.com/problems/mini-parser/
3+
4+
Given a nested list of integers represented as a string, implement a parser to deserialize it.
5+
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
6+
7+
Note: You may assume that the string is well-formed:
8+
String is non-empty.
9+
String does not contain white spaces.
10+
String contains only digits 0-9, [, - ,, ].
11+
12+
Example 1:
13+
Given s = "324",
14+
You should return a NestedInteger object which contains a single integer 324.
15+
16+
Example 2:
17+
Given s = "[123,[456,[789]]]",
18+
Return a NestedInteger object containing a nested list with 2 elements:
19+
1. An integer containing value 123.
20+
2. A nested list containing two elements:
21+
i. An integer containing value 456.
22+
ii. A nested list with one element:
23+
a. An integer containing value 789.
24+
"""
25+
# """
26+
# This is the interface that allows for creating nested lists.
27+
# You should not implement it, or speculate about its implementation
28+
# """
29+
#class NestedInteger:
30+
# def __init__(self, value=None):
31+
# """
32+
# If value is not specified, initializes an empty list.
33+
# Otherwise initializes a single integer equal to value.
34+
# """
35+
#
36+
# def isInteger(self):
37+
# """
38+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
39+
# :rtype bool
40+
# """
41+
#
42+
# def add(self, elem):
43+
# """
44+
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
45+
# :rtype void
46+
# """
47+
#
48+
# def setInteger(self, value):
49+
# """
50+
# Set this NestedInteger to hold a single integer equal to value.
51+
# :rtype void
52+
# """
53+
#
54+
# def getInteger(self):
55+
# """
56+
# @return the single integer that this NestedInteger holds, if it holds a single integer
57+
# Return None if this NestedInteger holds a nested list
58+
# :rtype int
59+
# """
60+
#
61+
# def getList(self):
62+
# """
63+
# @return the nested list that this NestedInteger holds, if it holds a nested list
64+
# Return None if this NestedInteger holds a single integer
65+
# :rtype List[NestedInteger]
66+
# """
67+
68+
class Solution:
69+
def deserialize(self, s: str) -> NestedInteger:
70+
stack, num, last = [], "", None
71+
for c in s:
72+
if c.isdigit() or c == "-":
73+
num += c
74+
75+
elif c == "," and num:
76+
stack[-1].add(NestedInteger(int(num)))
77+
num = ""
78+
79+
elif c == "[":
80+
elem = NestedInteger()
81+
if stack:
82+
stack[-1].add(elem)
83+
stack.append(elem)
84+
85+
elif c == "]":
86+
if num:
87+
stack[-1].add(NestedInteger(int(num)))
88+
num = ""
89+
last = stack.pop()
90+
91+
return last if last else NestedInteger(int(num))

0 commit comments

Comments
 (0)