|
| 1 | +package medium; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Stack; |
| 6 | + |
| 7 | +public class MiniParser { |
| 8 | + |
| 9 | + //Lessons: ask the interviewer to clarify the input, for this question, the input could be "324", "[324]", they are different |
| 10 | + //the former should return a nested integer with one single integer, the latter should return a nested integer with a list |
| 11 | + |
| 12 | + //Idea: |
| 13 | + //if it's '[', we just construct a new nested integer and push it onto the stack |
| 14 | + //if it's a number, we parse the whole number and add to the previous nested integer object |
| 15 | + //if it's ',', we'll just continue; |
| 16 | + //if it's ']', we'll just pop one nested integer from the working stack and assign it to the result |
| 17 | + |
| 18 | + public NestedInteger deserialize(String s) { |
| 19 | + if(s == null || s.isEmpty() || s.length() == 0) return new NestedInteger(); |
| 20 | + Stack<NestedInteger> workStack = new Stack<NestedInteger>(); |
| 21 | + NestedInteger result = null; |
| 22 | + StringBuilder sb = new StringBuilder(); |
| 23 | + int i = 0; |
| 24 | + //if it's just a single number, then we'll just return a nested integer with one integer |
| 25 | + if(s.charAt(i) != '['){ |
| 26 | + sb.setLength(0); |
| 27 | + while(i < s.length() && ((Character.getNumericValue(s.charAt(i)) < 10 && Character.getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')){ |
| 28 | + sb.append(s.charAt(i)); |
| 29 | + i++; |
| 30 | + } |
| 31 | + int num = Integer.parseInt(sb.toString()); |
| 32 | + return new NestedInteger(num); |
| 33 | + }//all other cases, we'll return a nested integer with a list |
| 34 | + else{ |
| 35 | + while (i < s.length()) { |
| 36 | + if (s.charAt(i) == '[') { |
| 37 | + NestedInteger ni = new NestedInteger(); |
| 38 | + // we'll put this one into its last one if there's one on the workStack |
| 39 | + if (!workStack.isEmpty()) { |
| 40 | + NestedInteger lastNi = workStack.pop(); |
| 41 | + lastNi.add(ni); |
| 42 | + workStack.push(lastNi);// then push it back |
| 43 | + } |
| 44 | + workStack.push(ni); |
| 45 | + i++; |
| 46 | + } else if (s.charAt(i) == ',') { |
| 47 | + i++; |
| 48 | + } else if (s.charAt(i) == ']') { |
| 49 | + NestedInteger completedNi = workStack.pop(); |
| 50 | + result = completedNi; |
| 51 | + i++; |
| 52 | + } else { |
| 53 | + // then it must be a number |
| 54 | + sb.setLength(0); |
| 55 | + while (i < s.length() |
| 56 | + && ((Character.getNumericValue(s.charAt(i)) < 10 && Character |
| 57 | + .getNumericValue(s.charAt(i)) >= 0) || s.charAt(i) == '-')) { |
| 58 | + sb.append(s.charAt(i)); |
| 59 | + i++; |
| 60 | + } |
| 61 | + int num = Integer.parseInt(sb.toString()); |
| 62 | + NestedInteger ni = null; |
| 63 | + if (!workStack.isEmpty()) |
| 64 | + ni = workStack.pop(); |
| 65 | + else |
| 66 | + ni = new NestedInteger(); |
| 67 | + // case 1: if this one contains one integer |
| 68 | + if (ni.isInteger()) { |
| 69 | + // we'll add it to this ni |
| 70 | + ni.add(new NestedInteger(num)); |
| 71 | + } |
| 72 | + // case 2: if this one contains a nested integer |
| 73 | + else if (ni.getList() != null && ni.getList().size() != 0) { |
| 74 | + // we'll get the last nested integer and add this one to it |
| 75 | + ni.add(new NestedInteger(num)); |
| 76 | + } else { |
| 77 | + // case 3: if this is an empty nested integer |
| 78 | + if(i > 0) ni.add(new NestedInteger(num)); |
| 79 | + else ni.setInteger(num); |
| 80 | + } |
| 81 | + workStack.push(ni); |
| 82 | + if (i == s.length()) |
| 83 | + return ni;// this is for test cases like this: "324", there's no '[' or ']' |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + public static void main(String...args){ |
| 91 | + MiniParser test = new MiniParser(); |
| 92 | +// String s = "[-1]"; |
| 93 | +// String s = "324"; |
| 94 | +// String s = "[]"; |
| 95 | +// String s = "[-1,-2]"; |
| 96 | + String s = "[-1,-2,[-3,-4,[5,[6,[7,8]]]]]"; |
| 97 | + NestedInteger result = test.deserialize(s); |
| 98 | + System.out.println(result.printNi(result, new StringBuilder())); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class NestedInteger { |
| 103 | + private List<NestedInteger> list; |
| 104 | + private Integer integer; |
| 105 | + |
| 106 | + public NestedInteger(List<NestedInteger> list){ |
| 107 | + this.list = list; |
| 108 | + } |
| 109 | + |
| 110 | + public void add(NestedInteger nestedInteger) { |
| 111 | + if(this.list != null){ |
| 112 | + this.list.add(nestedInteger); |
| 113 | + } else { |
| 114 | + this.list = new ArrayList(); |
| 115 | + this.list.add(nestedInteger); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public void setInteger(int num) { |
| 120 | + this.integer = num; |
| 121 | + } |
| 122 | + |
| 123 | + public NestedInteger(Integer integer){ |
| 124 | + this.integer = integer; |
| 125 | + } |
| 126 | + |
| 127 | + public NestedInteger() { |
| 128 | + this.list = new ArrayList(); |
| 129 | + } |
| 130 | + |
| 131 | + public boolean isInteger() { |
| 132 | + return integer != null; |
| 133 | + } |
| 134 | + |
| 135 | + public Integer getInteger() { |
| 136 | + return integer; |
| 137 | + } |
| 138 | + |
| 139 | + public List<NestedInteger> getList() { |
| 140 | + return list; |
| 141 | + } |
| 142 | + |
| 143 | + public String printNi(NestedInteger thisNi, StringBuilder sb){ |
| 144 | + if(thisNi.isInteger()) { |
| 145 | + sb.append(thisNi.integer); |
| 146 | + sb.append(","); |
| 147 | + } |
| 148 | + sb.append("["); |
| 149 | + for(NestedInteger ni : thisNi.list){ |
| 150 | + if(ni.isInteger()) { |
| 151 | + sb.append(ni.integer); |
| 152 | + sb.append(","); |
| 153 | + } |
| 154 | + else { |
| 155 | + printNi(ni, sb); |
| 156 | + } |
| 157 | + } |
| 158 | + sb.append("]"); |
| 159 | + return sb.toString(); |
| 160 | + } |
| 161 | +} |
| 162 | + |
0 commit comments