题目描述
思路
栈
从左往右遍历s,如果遇到’[‘则表示一个新的NestedInteger实例,需要入栈。如果遇到’]‘或’,’,则表示一个数字或NestedInteger实例结束,需要将其添加入栈顶的NestedInteger实例。最后返回栈顶实例即可。
Python实现
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger:
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """
#
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """
#
# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """
class Solution:
def deserialize(self, s: str) -> NestedInteger:
if s[0] != '[':
return NestedInteger(int(s))
stack, num, negative = [], 0, False
for i, c in enumerate(s):
if c == '-':
negative = True
elif c.isdigit():
num = num * 10 + int(c)
elif c == '[':
stack.append(NestedInteger())
elif c in ',]':
if s[i-1].isdigit():
if negative:
num = -num
stack[-1].add(NestedInteger(num))
num, negative = 0, False
if c == ']' and len(stack) > 1:
stack[-2].add(stack.pop())
return stack.pop()
Java实现
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return empty list if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class Solution {
public NestedInteger deserialize(String s) {
if (s.charAt(0) != '[') {
return new NestedInteger(Integer.parseInt(s));
}
Deque<NestedInteger> stack = new ArrayDeque<NestedInteger>();
int num = 0;
boolean negative = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '-') {
negative = true;
} else if (Character.isDigit(c)) {
num = num * 10 + c - '0';
} else if (c == '[') {
stack.push(new NestedInteger());
} else if (c == ',' || c == ']') {
if (Character.isDigit(s.charAt(i - 1))) {
if (negative) {
num *= -1;
}
stack.peek().add(new NestedInteger(num));
}
num = 0;
negative = false;
if (c == ']' && stack.size() > 1) {
NestedInteger ni = stack.pop();
stack.peek().add(ni);
}
}
}
return stack.pop();
}
}