|
55 | 55 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
56 | 56 |
|
57 | 57 | ```python
|
58 |
| - |
| 58 | +# """ |
| 59 | +# This is the interface that allows for creating nested lists. |
| 60 | +# You should not implement it, or speculate about its implementation |
| 61 | +# """ |
| 62 | +# class NestedInteger: |
| 63 | +# def __init__(self, value=None): |
| 64 | +# """ |
| 65 | +# If value is not specified, initializes an empty list. |
| 66 | +# Otherwise initializes a single integer equal to value. |
| 67 | +# """ |
| 68 | +# |
| 69 | +# def isInteger(self): |
| 70 | +# """ |
| 71 | +# @return True if this NestedInteger holds a single integer, rather than a nested list. |
| 72 | +# :rtype bool |
| 73 | +# """ |
| 74 | +# |
| 75 | +# def add(self, elem): |
| 76 | +# """ |
| 77 | +# Set this NestedInteger to hold a nested list and adds a nested integer elem to it. |
| 78 | +# :rtype void |
| 79 | +# """ |
| 80 | +# |
| 81 | +# def setInteger(self, value): |
| 82 | +# """ |
| 83 | +# Set this NestedInteger to hold a single integer equal to value. |
| 84 | +# :rtype void |
| 85 | +# """ |
| 86 | +# |
| 87 | +# def getInteger(self): |
| 88 | +# """ |
| 89 | +# @return the single integer that this NestedInteger holds, if it holds a single integer |
| 90 | +# Return None if this NestedInteger holds a nested list |
| 91 | +# :rtype int |
| 92 | +# """ |
| 93 | +# |
| 94 | +# def getList(self): |
| 95 | +# """ |
| 96 | +# @return the nested list that this NestedInteger holds, if it holds a nested list |
| 97 | +# Return None if this NestedInteger holds a single integer |
| 98 | +# :rtype List[NestedInteger] |
| 99 | +# """ |
| 100 | +class Solution: |
| 101 | + def deserialize(self, s: str) -> NestedInteger: |
| 102 | + if not s: |
| 103 | + return NestedInteger() |
| 104 | + if s[0] != '[': |
| 105 | + return NestedInteger(int(s)) |
| 106 | + if len(s) <= 2: |
| 107 | + return NestedInteger() |
| 108 | + ans = NestedInteger() |
| 109 | + depth, j = 0, 1 |
| 110 | + for i in range(1, len(s)): |
| 111 | + if depth == 0 and (s[i] == ',' or i == len(s) - 1): |
| 112 | + ans.add(self.deserialize(s[j:i])) |
| 113 | + j = i + 1 |
| 114 | + elif s[i] == '[': |
| 115 | + depth += 1 |
| 116 | + elif s[i] == ']': |
| 117 | + depth -= 1 |
| 118 | + return ans |
59 | 119 | ```
|
60 | 120 |
|
61 | 121 | ### **Java**
|
62 | 122 |
|
63 | 123 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
64 | 124 |
|
65 | 125 | ```java
|
| 126 | +/** |
| 127 | + * // This is the interface that allows for creating nested lists. |
| 128 | + * // You should not implement it, or speculate about its implementation |
| 129 | + * public interface NestedInteger { |
| 130 | + * // Constructor initializes an empty nested list. |
| 131 | + * public NestedInteger(); |
| 132 | + * |
| 133 | + * // Constructor initializes a single integer. |
| 134 | + * public NestedInteger(int value); |
| 135 | + * |
| 136 | + * // @return true if this NestedInteger holds a single integer, rather than a nested list. |
| 137 | + * public boolean isInteger(); |
| 138 | + * |
| 139 | + * // @return the single integer that this NestedInteger holds, if it holds a single integer |
| 140 | + * // Return null if this NestedInteger holds a nested list |
| 141 | + * public Integer getInteger(); |
| 142 | + * |
| 143 | + * // Set this NestedInteger to hold a single integer. |
| 144 | + * public void setInteger(int value); |
| 145 | + * |
| 146 | + * // Set this NestedInteger to hold a nested list and adds a nested integer to it. |
| 147 | + * public void add(NestedInteger ni); |
| 148 | + * |
| 149 | + * // @return the nested list that this NestedInteger holds, if it holds a nested list |
| 150 | + * // Return empty list if this NestedInteger holds a single integer |
| 151 | + * public List<NestedInteger> getList(); |
| 152 | + * } |
| 153 | + */ |
| 154 | +class Solution { |
| 155 | + public NestedInteger deserialize(String s) { |
| 156 | + if ("".equals(s)) { |
| 157 | + return new NestedInteger(); |
| 158 | + } |
| 159 | + if (s.charAt(0) != '[') { |
| 160 | + return new NestedInteger(Integer.parseInt(s)); |
| 161 | + } |
| 162 | + if (s.length() <= 2) { |
| 163 | + return new NestedInteger(); |
| 164 | + } |
| 165 | + NestedInteger ans = new NestedInteger(); |
| 166 | + int depth = 0; |
| 167 | + for (int i = 1, j = 1; i < s.length(); ++i) { |
| 168 | + if (depth == 0 && (s.charAt(i) == ',' || i == s.length() - 1)) { |
| 169 | + ans.add(deserialize(s.substring(j, i))); |
| 170 | + j = i + 1; |
| 171 | + } else if (s.charAt(i) == '[') { |
| 172 | + ++depth; |
| 173 | + } else if (s.charAt(i) == ']') { |
| 174 | + --depth; |
| 175 | + } |
| 176 | + } |
| 177 | + return ans; |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
66 | 181 |
|
| 182 | +### **C++** |
| 183 | + |
| 184 | +```cpp |
| 185 | +/** |
| 186 | + * // This is the interface that allows for creating nested lists. |
| 187 | + * // You should not implement it, or speculate about its implementation |
| 188 | + * class NestedInteger { |
| 189 | + * public: |
| 190 | + * // Constructor initializes an empty nested list. |
| 191 | + * NestedInteger(); |
| 192 | + * |
| 193 | + * // Constructor initializes a single integer. |
| 194 | + * NestedInteger(int value); |
| 195 | + * |
| 196 | + * // Return true if this NestedInteger holds a single integer, rather than a nested list. |
| 197 | + * bool isInteger() const; |
| 198 | + * |
| 199 | + * // Return the single integer that this NestedInteger holds, if it holds a single integer |
| 200 | + * // The result is undefined if this NestedInteger holds a nested list |
| 201 | + * int getInteger() const; |
| 202 | + * |
| 203 | + * // Set this NestedInteger to hold a single integer. |
| 204 | + * void setInteger(int value); |
| 205 | + * |
| 206 | + * // Set this NestedInteger to hold a nested list and adds a nested integer to it. |
| 207 | + * void add(const NestedInteger &ni); |
| 208 | + * |
| 209 | + * // Return the nested list that this NestedInteger holds, if it holds a nested list |
| 210 | + * // The result is undefined if this NestedInteger holds a single integer |
| 211 | + * const vector<NestedInteger> &getList() const; |
| 212 | + * }; |
| 213 | + */ |
| 214 | +class Solution { |
| 215 | +public: |
| 216 | + NestedInteger deserialize(string s) { |
| 217 | + if (s.empty()) return NestedInteger(); |
| 218 | + if (s[0] != '[') return NestedInteger(stoi(s)); |
| 219 | + if (s.size() <= 2) return NestedInteger(); |
| 220 | + NestedInteger ans; |
| 221 | + int depth = 0; |
| 222 | + for (int i = 1, j = 1; i < s.size(); ++i) |
| 223 | + { |
| 224 | + if (depth == 0 && (s[i] == ',' || i == s.size() - 1)) |
| 225 | + { |
| 226 | + ans.add(deserialize(s.substr(j, i - j))); |
| 227 | + j = i + 1; |
| 228 | + } |
| 229 | + else if (s[i] == '[') ++depth; |
| 230 | + else if (s[i] == ']') --depth; |
| 231 | + } |
| 232 | + return ans; |
| 233 | + } |
| 234 | +}; |
67 | 235 | ```
|
68 | 236 |
|
69 | 237 | ### **...**
|
|
0 commit comments