4
4
5
5
import java .util .Stack ;
6
6
7
- /**
8
- * 385. Mini Parser
9
- *
10
- * Given a nested list of integers represented as a string,
11
- * implement a parser to deserialize it.
12
-
13
- Each element is either an integer, or a list --
14
- whose elements may also be integers or other lists.
15
-
16
- Note: You may assume that the string is well-formed:
17
-
18
- String is non-empty.
19
- String does not contain white spaces.
20
- String contains only digits 0-9, [, - ,, ].
21
- Example 1:
22
-
23
- Given s = "324",
24
-
25
- You should return a NestedInteger object which contains a single integer 324.
26
- Example 2:
27
-
28
- Given s = "[123,[456,[789]]]",
29
-
30
- Return a NestedInteger object containing a nested list with 2 elements:
31
-
32
- 1. An integer containing value 123.
33
- 2. A nested list containing two elements:
34
- i. An integer containing value 456.
35
- ii. A nested list with one element:
36
- a. An integer containing value 789.*/
37
7
public class _385 {
38
8
39
9
public static class Solution1 {
@@ -58,7 +28,7 @@ public NestedInteger deserialize(String s) {
58
28
if (s .charAt (i ) != '[' ) {
59
29
sb .setLength (0 );
60
30
while (i < s .length () && ((Character .getNumericValue (s .charAt (i )) < 10
61
- && Character .getNumericValue (s .charAt (i )) >= 0 ) || s .charAt (i ) == '-' )) {
31
+ && Character .getNumericValue (s .charAt (i )) >= 0 ) || s .charAt (i ) == '-' )) {
62
32
sb .append (s .charAt (i ));
63
33
i ++;
64
34
}
@@ -87,8 +57,8 @@ public NestedInteger deserialize(String s) {
87
57
// then it must be a number
88
58
sb .setLength (0 );
89
59
while (i < s .length ()
90
- && ((Character .getNumericValue (s .charAt (i )) < 10 && Character
91
- .getNumericValue (s .charAt (i )) >= 0 ) || s .charAt (i ) == '-' )) {
60
+ && ((Character .getNumericValue (s .charAt (i )) < 10 && Character
61
+ .getNumericValue (s .charAt (i )) >= 0 ) || s .charAt (i ) == '-' )) {
92
62
sb .append (s .charAt (i ));
93
63
i ++;
94
64
}
0 commit comments