This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
mini-parser.go
executable file
·110 lines (92 loc) · 2.38 KB
/
mini-parser.go
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
package problem0385
import (
"strconv"
"github.com/aQuaYi/LeetCode-in-Go/kit"
)
/**
* // 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 {}
*/
type NestedInteger = kit.NestedInteger
func deserialize(s string) *NestedInteger {
if len(s) == 0 {
return nil
}
if s[0] != '[' {
return getValue(s)
}
stack := new(stackChars)
var cur *NestedInteger
si, ci := 0, 0
for ; ci < len(s); ci++ {
switch s[ci] {
case '[':
if cur != nil {
stack.Push(cur)
}
cur = new(NestedInteger)
si = ci + 1
case ']':
if ci > si {
cur.Add(*getValue(s[si:ci]))
}
if !stack.Empty() {
tmp := stack.Pop()
tmp.Add(*cur)
cur = tmp
}
si = ci + 1
case ',':
if s[ci-1] != ']' {
cur.Add(*getValue(s[si:ci]))
}
si = ci + 1
}
}
return cur
}
func getValue(s string) *NestedInteger {
val, _ := strconv.Atoi(s)
item := new(NestedInteger)
item.SetInteger(val)
return item
}
type stackChars struct {
chars []*NestedInteger
}
func (s *stackChars) Push(nb *NestedInteger) {
s.chars = append(s.chars, nb)
}
func (s *stackChars) Pop() *NestedInteger {
slen := len(s.chars)
rb := s.chars[slen-1]
s.chars = s.chars[:slen-1]
return rb
}
func (s *stackChars) Empty() bool {
return len(s.chars) == 0
}
// func (s *stackChars) Peek() *NestedInteger {
// return s.chars[len(s.chars)-1]
// }