-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path385.java
153 lines (149 loc) · 5.21 KB
/
385.java
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
__________________________________________________________________________________________________
sample 2 ms submission
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class Solution {
int current = 0;
String str;
public NestedInteger deserialize(String s) {
this.current = 0;
this.str = s;
return this.expr();
}
private NestedInteger expr() {
NestedInteger ni = new NestedInteger();
if(str.charAt(current) != '[') {
ni.setInteger(parseInt());
return ni;
}
if(str.charAt(current+1) == ']') {
current = current + 2;
return new NestedInteger();
}
return this.nested();
}
private NestedInteger nested() {
NestedInteger ni = new NestedInteger();
if(str.charAt(current++) != '[') {} // error
ni.add(this.expr());
while(str.charAt(current) == ',') {
++current;
ni.add(this.expr());
}
if(str.charAt(current++) != ']') {} // error
return ni;
}
private int parseInt() {
boolean negative = false;
if(str.charAt(current) == '-') {
negative = true;
++current;
}
int i = 0;
while(current < str.length() && str.charAt(current) - '0' >= 0 && str.charAt(current) - '0' <= 9) {
i = i * 10 + str.charAt(current) - '0';
current = current + 1;
}
return negative ? -1*i : i;
}
}
__________________________________________________________________________________________________
sample 38448 kb submission
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class Solution {
public NestedInteger deserialize(String s) {
if(s.charAt(0) != '[')
return new NestedInteger(Integer.valueOf(s));
Stack<NestedInteger> st = new Stack<>();
int minus = 1;
Integer num = null;
NestedInteger ni = null;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '[') {
if (ni != null)
st.push(ni);
ni = new NestedInteger();
}
else if(s.charAt(i) == ']') {
if(num != null)
ni.add(new NestedInteger(num*minus));
if (!st.isEmpty()) {
st.peek().add(ni);
ni = st.pop();
}
minus = 1;
num = null;
}
else if(s.charAt(i) == ',') {
if(num != null)
ni.add(new NestedInteger(num*minus));
minus = 1;
num = null;
}
else if(s.charAt(i) == '-') {
minus = -1;
}
else {
if (num == null)
num = s.charAt(i) - '0';
else
num = num * 10 + (s.charAt(i) - '0');
}
}
return ni;
}
}
__________________________________________________________________________________________________