|
4 | 4 |
|
5 | 5 | import java.util.Stack;
|
6 | 6 |
|
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. |
8 | 12 |
|
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. |
10 | 15 |
|
11 | 16 | Note: You may assume that the string is well-formed:
|
12 | 17 |
|
|
30 | 35 | ii. A nested list with one element:
|
31 | 36 | a. An integer containing value 789.*/
|
32 | 37 | 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 |
36 | 38 |
|
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 |
42 | 42 |
|
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(); |
57 | 52 | }
|
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)); |
72 | 63 | 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; |
86 | 85 | 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)); |
103 | 86 | } 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 |
106 | 109 | ni.add(new NestedInteger(num));
|
107 | 110 | } 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 ']' |
109 | 121 | }
|
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 ']' |
114 | 122 | }
|
115 | 123 | }
|
116 | 124 | }
|
| 125 | + return result; |
117 | 126 | }
|
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())); |
130 | 127 | }
|
131 | 128 | }
|
0 commit comments