|
| 1 | +import java.util.List; |
| 2 | +import java.util.Stack; |
| 3 | + |
| 4 | +/** |
| 5 | + * Given a nested list of integers represented as a string, implement a parser to deserialize it. |
| 6 | + * Each element is either an integer, or a list -- whose elements may also be integers or other lists. |
| 7 | + * <p> |
| 8 | + * Note: You may assume that the string is well-formed: |
| 9 | + * String is non-empty. |
| 10 | + * String does not contain white spaces. |
| 11 | + * String contains only digits 0-9, [, - ,, ]. |
| 12 | + * <p> |
| 13 | + * Example 1: |
| 14 | + * Given s = "324", |
| 15 | + * You should return a NestedInteger object which contains a single integer 324. |
| 16 | + * <p> |
| 17 | + * Example 2: |
| 18 | + * Given s = "[123,[456,[789]]]", |
| 19 | + * Return a NestedInteger object containing a nested list with 2 elements: |
| 20 | + * <p> |
| 21 | + * 1. An integer containing value 123. |
| 22 | + * 2. A nested list containing two elements: |
| 23 | + * i. An integer containing value 456. |
| 24 | + * ii. A nested list with one element: |
| 25 | + * a. An integer containing value 789. |
| 26 | + * <p> |
| 27 | + * Created by drfish on 6/9/2017. |
| 28 | + */ |
| 29 | +public class _385MiniParser { |
| 30 | + /** |
| 31 | + * // This is the interface that allows for creating nested lists. |
| 32 | + * // You should not implement it, or speculate about its implementation |
| 33 | + **/ |
| 34 | + public interface INestedInteger { |
| 35 | +// // Constructor initializes an empty nested list. |
| 36 | +// public NestedInteger(); |
| 37 | +// |
| 38 | +// // Constructor initializes a single integer. |
| 39 | +// public NestedInteger(int value); |
| 40 | + |
| 41 | + // @return true if this NestedInteger holds a single integer, rather than a nested list. |
| 42 | + public boolean isInteger(); |
| 43 | + |
| 44 | + // @return the single integer that this NestedInteger holds, if it holds a single integer |
| 45 | + // Return null if this NestedInteger holds a nested list |
| 46 | + public Integer getInteger(); |
| 47 | + |
| 48 | + // Set this NestedInteger to hold a single integer. |
| 49 | + public void setInteger(int value); |
| 50 | + |
| 51 | + // Set this NestedInteger to hold a nested list and adds a nested integer to it. |
| 52 | + public void add(NestedInteger ni); |
| 53 | + |
| 54 | + // @return the nested list that this NestedInteger holds, if it holds a nested list |
| 55 | + // Return null if this NestedInteger holds a single integer |
| 56 | + public List<NestedInteger> getList(); |
| 57 | + } |
| 58 | + |
| 59 | + public class NestedInteger implements INestedInteger { |
| 60 | + public NestedInteger() { |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + public NestedInteger(int value) { |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isInteger() { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Integer getInteger() { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void setInteger(int value) { |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void add(NestedInteger ni) { |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public List<NestedInteger> getList() { |
| 90 | + return null; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public class Solution { |
| 95 | + public NestedInteger deserialize(String s) { |
| 96 | + if (s == null || s.isEmpty()) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + if (s.charAt(0) != '[') { |
| 100 | + return new NestedInteger(Integer.valueOf(s)); |
| 101 | + } |
| 102 | + Stack<NestedInteger> stack = new Stack<>(); |
| 103 | + int start = 0; |
| 104 | + NestedInteger curr = null; |
| 105 | + for (int i = 0; i < s.length(); i++) { |
| 106 | + char c = s.charAt(i); |
| 107 | + if (c == '[') { |
| 108 | + if (curr != null) { |
| 109 | + stack.push(curr); |
| 110 | + } |
| 111 | + curr = new NestedInteger(); |
| 112 | + start = i + 1; |
| 113 | + } else if (c == ']') { |
| 114 | + String num = s.substring(start, i); |
| 115 | + if (!num.isEmpty()) { |
| 116 | + curr.add(new NestedInteger(Integer.valueOf(num))); |
| 117 | + } |
| 118 | + if (!stack.isEmpty()) { |
| 119 | + NestedInteger top = stack.pop(); |
| 120 | + top.add(curr); |
| 121 | + curr = top; |
| 122 | + } |
| 123 | + start = i + 1; |
| 124 | + } else if (c == ',') { |
| 125 | + if (s.charAt(i - 1) != ']') { |
| 126 | + String num = s.substring(start, i); |
| 127 | + curr.add(new NestedInteger(Integer.valueOf(num))); |
| 128 | + } |
| 129 | + start = i + 1; |
| 130 | + } |
| 131 | + } |
| 132 | + return curr; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments