-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path385-MiniParser.go
More file actions
186 lines (170 loc) · 5.62 KB
/
385-MiniParser.go
File metadata and controls
186 lines (170 loc) · 5.62 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
// 385. Mini Parser
// Given a string s represents the serialization of a nested list,
// implement a parser to deserialize it and return the deserialized NestedInteger.
// Each element is either an integer or a list whose elements may also be integers or other lists.
// Example 1:
// Input: s = "324"
// Output: 324
// Explanation: You should return a NestedInteger object which contains a single integer 324.
// Example 2:
// Input: s = "[123,[456,[789]]]"
// Output: [123,[456,[789]]]
// Explanation: Return a NestedInteger object containing a nested list with 2 elements:
// 1. An integer containing value 123.
// 2. A nested list containing two elements:
// i. An integer containing value 456.
// ii. A nested list with one element:
// a. An integer containing value 789
// Constraints:
// 1 <= s.length <= 5 * 10^4
// s consists of digits, square brackets "[]", negative sign '-', and commas ','.
// s is the serialization of valid NestedInteger.
// All the values in the input are in the range [-10^6, 10^6].
import "fmt"
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* type NestedInteger struct {
* }
*
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* func (n NestedInteger) IsInteger() bool {}
*
* // 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
* // So before calling this method, you should have a check
* func (n NestedInteger) GetInteger() int {}
*
* // Set this NestedInteger to hold a single integer.
* func (n *NestedInteger) SetInteger(value int) {}
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* func (n *NestedInteger) Add(elem NestedInteger) {}
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The list length is zero if this NestedInteger holds a single integer
* // You can access NestedInteger's List element directly if you want to modify it
* func (n NestedInteger) GetList() []*NestedInteger {}
*/
// 根据题意自己构造NestedInteger的构造函数
// 创建一个空的NestedInteger对象
func NewNestedInteger() NestedInteger {
return NestedInteger{}
}
// 创建一个带数字的NestedInteger对象
func NewNestedIntegerWithNum(i int) NestedInteger {
obj := NestedInteger{}
obj.SetInteger(i)
return obj
}
type NestedInteger struct {
num int
inner []*NestedInteger
}
func (this *NestedInteger) SetInteger(value int) {
this.num = value
}
func (this *NestedInteger) Add(elem NestedInteger) {
this.inner = append(this.inner, &elem)
}
func (this NestedInteger) GetList() []*NestedInteger {
return this.inner
}
func deserialize(s string) *NestedInteger {
stack := []NestedInteger{ NestedInteger{} }
for i := 0; i < len(s); {
if s[i] == ',' {
i++
continue
}
if s[i] == '[' {
stack = append(stack, NestedInteger{})
i++
continue
} else if s[i] == ']' {
s := stack[len(stack)-1]
stack = stack[:len(stack)-1]
stack[len(stack)-1].Add(s)
i++
continue
}
if s[i] == '-' || (s[i] >= '0' && s[i] <= '9') {
var temp int
var minus bool
if s[i] == '-' {
minus = true
i++
}
for i < len(s) && s[i] != ',' && s[i] != ']' {
temp = temp*10 + int(s[i]-'0')
i++
}
if minus {
temp = -temp
}
var new NestedInteger
new.SetInteger(temp)
stack[len(stack)-1].Add(new)
}
}
return stack[0].GetList()[0]
}
func deserialize1(s string) *NestedInteger {
stack:=[]NestedInteger{NestedInteger{}}
for i:=0;i<len(s);{
if s[i]==','{
i++
continue
}
if s[i]=='['{
stack=append(stack,NestedInteger{})
i++
continue
}else if s[i]==']'{
top:=stack[len(stack)-1]
stack=stack[:len(stack)-1]
stack[len(stack)-1].Add(top)
i++
continue
}
if s[i]=='-'|| (s[i]>='0' && s[i]<='9'){
var temp int
var minus bool
if s[i]=='-'{
minus=true
i++
}
for i<len(s) && s[i]!=',' && s[i]!=']'{
temp=temp*10+int(s[i]-'0')
i++
}
if minus{
temp=-temp
}
var new NestedInteger
new.SetInteger(temp)
stack[len(stack)-1].Add(new)
}
}
return stack[0].GetList()[0]
}
func main() {
// Example 1:
// Input: s = "324"
// Output: 324
// Explanation: You should return a NestedInteger object which contains a single integer 324.
fmt.Println(deserialize("324"))
// Example 2:
// Input: s = "[123,[456,[789]]]"
// Output: [123,[456,[789]]]
// Explanation: Return a NestedInteger object containing a nested list with 2 elements:
// 1. An integer containing value 123.
// 2. A nested list containing two elements:
// i. An integer containing value 456.
// ii. A nested list with one element:
// a. An integer containing value 789
fmt.Println(deserialize("[123,[456,[789]]]"))
fmt.Println(deserialize1("324"))
fmt.Println(deserialize1("[123,[456,[789]]]"))
}