|
| 1 | +packagemedium; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.List; |
| 5 | +importjava.util.Stack; |
| 6 | + |
| 7 | +publicclassMiniParser { |
| 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 | +publicNestedIntegerdeserialize(Strings) { |
| 19 | +if(s ==null ||s.isEmpty() ||s.length() ==0)returnnewNestedInteger(); |
| 20 | +Stack<NestedInteger>workStack =newStack<NestedInteger>(); |
| 21 | +NestedIntegerresult =null; |
| 22 | +StringBuildersb =newStringBuilder(); |
| 23 | +inti =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 | +intnum =Integer.parseInt(sb.toString()); |
| 32 | +returnnewNestedInteger(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 | +NestedIntegerni =newNestedInteger(); |
| 38 | +// we'll put this one into its last one if there's one on the workStack |
| 39 | +if (!workStack.isEmpty()) { |
| 40 | +NestedIntegerlastNi =workStack.pop(); |
| 41 | +lastNi.add(ni); |
| 42 | +workStack.push(lastNi);// then push it back |
| 43 | + } |
| 44 | +workStack.push(ni); |
| 45 | +i++; |
| 46 | + }elseif (s.charAt(i) ==',') { |
| 47 | +i++; |
| 48 | + }elseif (s.charAt(i) ==']') { |
| 49 | +NestedIntegercompletedNi =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 | +intnum =Integer.parseInt(sb.toString()); |
| 62 | +NestedIntegerni =null; |
| 63 | +if (!workStack.isEmpty()) |
| 64 | +ni =workStack.pop(); |
| 65 | +else |
| 66 | +ni =newNestedInteger(); |
| 67 | +// case 1: if this one contains one integer |
| 68 | +if (ni.isInteger()) { |
| 69 | +// we'll add it to this ni |
| 70 | +ni.add(newNestedInteger(num)); |
| 71 | + } |
| 72 | +// case 2: if this one contains a nested integer |
| 73 | +elseif (ni.getList() !=null &&ni.getList().size() !=0) { |
| 74 | +// we'll get the last nested integer and add this one to it |
| 75 | +ni.add(newNestedInteger(num)); |
| 76 | + }else { |
| 77 | +// case 3: if this is an empty nested integer |
| 78 | +if(i >0)ni.add(newNestedInteger(num)); |
| 79 | +elseni.setInteger(num); |
| 80 | + } |
| 81 | +workStack.push(ni); |
| 82 | +if (i ==s.length()) |
| 83 | +returnni;// this is for test cases like this: "324", there's no '[' or ']' |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +returnresult; |
| 88 | + } |
| 89 | + |
| 90 | +publicstaticvoidmain(String...args){ |
| 91 | +MiniParsertest =newMiniParser(); |
| 92 | +// String s = "[-1]"; |
| 93 | +// String s = "324"; |
| 94 | +// String s = "[]"; |
| 95 | +// String s = "[-1,-2]"; |
| 96 | +Strings ="[-1,-2,[-3,-4,[5,[6,[7,8]]]]]"; |
| 97 | +NestedIntegerresult =test.deserialize(s); |
| 98 | +System.out.println(result.printNi(result,newStringBuilder())); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +classNestedInteger { |
| 103 | +privateList<NestedInteger>list; |
| 104 | +privateIntegerinteger; |
| 105 | + |
| 106 | +publicNestedInteger(List<NestedInteger>list){ |
| 107 | +this.list =list; |
| 108 | + } |
| 109 | + |
| 110 | +publicvoidadd(NestedIntegernestedInteger) { |
| 111 | +if(this.list !=null){ |
| 112 | +this.list.add(nestedInteger); |
| 113 | + }else { |
| 114 | +this.list =newArrayList(); |
| 115 | +this.list.add(nestedInteger); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +publicvoidsetInteger(intnum) { |
| 120 | +this.integer =num; |
| 121 | + } |
| 122 | + |
| 123 | +publicNestedInteger(Integerinteger){ |
| 124 | +this.integer =integer; |
| 125 | + } |
| 126 | + |
| 127 | +publicNestedInteger() { |
| 128 | +this.list =newArrayList(); |
| 129 | + } |
| 130 | + |
| 131 | +publicbooleanisInteger() { |
| 132 | +returninteger !=null; |
| 133 | + } |
| 134 | + |
| 135 | +publicIntegergetInteger() { |
| 136 | +returninteger; |
| 137 | + } |
| 138 | + |
| 139 | +publicList<NestedInteger>getList() { |
| 140 | +returnlist; |
| 141 | + } |
| 142 | + |
| 143 | +publicStringprintNi(NestedIntegerthisNi,StringBuildersb){ |
| 144 | +if(thisNi.isInteger()) { |
| 145 | +sb.append(thisNi.integer); |
| 146 | +sb.append(","); |
| 147 | + } |
| 148 | +sb.append("["); |
| 149 | +for(NestedIntegerni :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 | +returnsb.toString(); |
| 160 | + } |
| 161 | +} |
| 162 | + |