Skip to content

Commit b0983ef

Browse files
refactor 385
1 parent a9592ed commit b0983ef

File tree

2 files changed

+123
-86
lines changed

2 files changed

+123
-86
lines changed

src/main/java/com/fishercoder/solutions/_385.java

Lines changed: 83 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
import java.util.Stack;
66

7-
/**Given a nested list of integers represented as a string, implement a parser to deserialize it.
7+
/**
8+
* 385. Mini Parser
9+
*
10+
* Given a nested list of integers represented as a string,
11+
* implement a parser to deserialize it.
812
9-
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
13+
Each element is either an integer, or a list --
14+
whose elements may also be integers or other lists.
1015
1116
Note: You may assume that the string is well-formed:
1217
@@ -30,102 +35,94 @@
3035
ii. A nested list with one element:
3136
a. An integer containing value 789.*/
3237
public class _385 {
33-
34-
//Lessons: ask the interviewer to clarify the input, for this question, the input could be "324", "[324]", they are different
35-
//the former should return a nested integer with one single integer, the latter should return a nested integer with a list
3638

37-
//Idea:
38-
//if it's '[', we just construct a new nested integer and push it onto the stack
39-
//if it's a number, we parse the whole number and add to the previous nested integer object
40-
//if it's ',', we'll just continue;
41-
//if it's ']', we'll just pop one nested integer from the working stack and assign it to the result
39+
public static class Solution1 {
40+
//Lessons: ask the interviewer to clarify the input, for this question, the input could be "324", "[324]", they are different
41+
//the former should return a nested integer with one single integer, the latter should return a nested integer with a list
4242

43-
public NestedInteger deserialize(String s) {
44-
if (s == null || s.isEmpty() || s.length() == 0) {
45-
return new NestedInteger();
46-
}
47-
Stack<NestedInteger> workStack = new Stack<NestedInteger>();
48-
NestedInteger result = null;
49-
StringBuilder sb = new StringBuilder();
50-
int i = 0;
51-
//if it's just a single number, then we'll just return a nested integer with one integer
52-
if (s.charAt(i) != '[') {
53-
sb.setLength(0);
54-
while (i < s.length() && ((Character.getNumericValue(s.charAt(i)) < 10 && Character.getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')) {
55-
sb.append(s.charAt(i));
56-
i++;
43+
//Idea:
44+
//if it's '[', we just construct a new nested integer and push it onto the stack
45+
//if it's a number, we parse the whole number and add to the previous nested integer object
46+
//if it's ',', we'll just continue;
47+
//if it's ']', we'll just pop one nested integer from the working stack and assign it to the result
48+
49+
public NestedInteger deserialize(String s) {
50+
if (s == null || s.isEmpty() || s.length() == 0) {
51+
return new NestedInteger();
5752
}
58-
int num = Integer.parseInt(sb.toString());
59-
return new NestedInteger(num);
60-
} else {
61-
//all other cases, we'll return a nested integer with a list
62-
while (i < s.length()) {
63-
if (s.charAt(i) == '[') {
64-
NestedInteger ni = new NestedInteger();
65-
// we'll put this one into its last one if there's one on the workStack
66-
if (!workStack.isEmpty()) {
67-
NestedInteger lastNi = workStack.pop();
68-
lastNi.add(ni);
69-
workStack.push(lastNi);// then push it back
70-
}
71-
workStack.push(ni);
53+
Stack<NestedInteger> workStack = new Stack<>();
54+
NestedInteger result = null;
55+
StringBuilder sb = new StringBuilder();
56+
int i = 0;
57+
//if it's just a single number, then we'll just return a nested integer with one integer
58+
if (s.charAt(i) != '[') {
59+
sb.setLength(0);
60+
while (i < s.length() && ((Character.getNumericValue(s.charAt(i)) < 10
61+
&& Character.getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')) {
62+
sb.append(s.charAt(i));
7263
i++;
73-
} else if (s.charAt(i) == ',') {
74-
i++;
75-
} else if (s.charAt(i) == ']') {
76-
NestedInteger completedNi = workStack.pop();
77-
result = completedNi;
78-
i++;
79-
} else {
80-
// then it must be a number
81-
sb.setLength(0);
82-
while (i < s.length()
83-
&& ((Character.getNumericValue(s.charAt(i)) < 10 && Character
84-
.getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')) {
85-
sb.append(s.charAt(i));
64+
}
65+
int num = Integer.parseInt(sb.toString());
66+
return new NestedInteger(num);
67+
} else {
68+
//all other cases, we'll return a nested integer with a list
69+
while (i < s.length()) {
70+
if (s.charAt(i) == '[') {
71+
NestedInteger ni = new NestedInteger();
72+
// we'll put this one into its last one if there's one on the workStack
73+
if (!workStack.isEmpty()) {
74+
NestedInteger lastNi = workStack.pop();
75+
lastNi.add(ni);
76+
workStack.push(lastNi);// then push it back
77+
}
78+
workStack.push(ni);
79+
i++;
80+
} else if (s.charAt(i) == ',') {
81+
i++;
82+
} else if (s.charAt(i) == ']') {
83+
NestedInteger completedNi = workStack.pop();
84+
result = completedNi;
8685
i++;
87-
}
88-
int num = Integer.parseInt(sb.toString());
89-
NestedInteger ni = null;
90-
if (!workStack.isEmpty()) {
91-
ni = workStack.pop();
92-
} else {
93-
ni = new NestedInteger();
94-
}
95-
// case 1: if this one contains one integer
96-
if (ni.isInteger()) {
97-
// we'll add it to this ni
98-
ni.add(new NestedInteger(num));
99-
} else if (ni.getList() != null && ni.getList().size() != 0) {
100-
// case 2: if this one contains a nested integer
101-
// we'll get the last nested integer and add this one to it
102-
ni.add(new NestedInteger(num));
10386
} else {
104-
// case 3: if this is an empty nested integer
105-
if (i > 0) {
87+
// then it must be a number
88+
sb.setLength(0);
89+
while (i < s.length()
90+
&& ((Character.getNumericValue(s.charAt(i)) < 10 && Character
91+
.getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')) {
92+
sb.append(s.charAt(i));
93+
i++;
94+
}
95+
int num = Integer.parseInt(sb.toString());
96+
NestedInteger ni = null;
97+
if (!workStack.isEmpty()) {
98+
ni = workStack.pop();
99+
} else {
100+
ni = new NestedInteger();
101+
}
102+
// case 1: if this one contains one integer
103+
if (ni.isInteger()) {
104+
// we'll add it to this ni
105+
ni.add(new NestedInteger(num));
106+
} else if (ni.getList() != null && ni.getList().size() != 0) {
107+
// case 2: if this one contains a nested integer
108+
// we'll get the last nested integer and add this one to it
106109
ni.add(new NestedInteger(num));
107110
} else {
108-
ni.setInteger(num);
111+
// case 3: if this is an empty nested integer
112+
if (i > 0) {
113+
ni.add(new NestedInteger(num));
114+
} else {
115+
ni.setInteger(num);
116+
}
117+
}
118+
workStack.push(ni);
119+
if (i == s.length()) {
120+
return ni;// this is for test cases like this: "324", there's no '[' or ']'
109121
}
110-
}
111-
workStack.push(ni);
112-
if (i == s.length()) {
113-
return ni;// this is for test cases like this: "324", there's no '[' or ']'
114122
}
115123
}
116124
}
125+
return result;
117126
}
118-
return result;
119-
}
120-
121-
public static void main(String... args) {
122-
_385 test = new _385();
123-
// String s = "[-1]";
124-
// String s = "324";
125-
// String s = "[]";
126-
// String s = "[-1,-2]";
127-
String s = "[-1,-2,[-3,-4,[5,[6,[7,8]]]]]";
128-
NestedInteger result = test.deserialize(s);
129-
System.out.println(result.printNi(result, new StringBuilder()));
130127
}
131128
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._385;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
public class _385Test {
8+
private static _385.Solution1 solution1;
9+
10+
@BeforeClass
11+
public static void setUp() {
12+
solution1 = new _385.Solution1();
13+
}
14+
15+
@Test
16+
public void test1() {
17+
solution1.deserialize("324");
18+
}
19+
20+
@Test
21+
public void test2() {
22+
solution1.deserialize("[-1]");
23+
}
24+
25+
@Test
26+
public void test3() {
27+
solution1.deserialize("[]");
28+
}
29+
30+
@Test
31+
public void test4() {
32+
solution1.deserialize("[-1,-2]");
33+
}
34+
35+
@Test
36+
public void test5() {
37+
solution1.deserialize("[-1,-2,[-3,-4,[5,[6,[7,8]]]]]");
38+
}
39+
40+
}

0 commit comments

Comments
 (0)