Given a nested list of integers represented as a string, implement a parser to deserialize it.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Note: You may assume that the string is well-formed:
String is non-empty.
String does not contain white spaces.
String contains only digits 0-9, [, - ,, ].
Example 1:
Given s = "324",
You should return a NestedInteger object which contains a single integer 324.
Example 2:
Given s = "[123,[456,[789]]]",
Return a NestedInteger object containing a nested list with 2 elements:
1. An integer containing value 123.
2. A nested list containing two elements:
i. An integer containing value 456.
ii. A nested list with one element:
a. An integer containing value 789.
这题的类的定义如下:
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* public:
* // Constructor initializes an empty nested list.
* NestedInteger();
*
* // Constructor initializes a single integer.
* NestedInteger(int value);
*
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* bool isInteger() const;
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* int getInteger() const;
*
* // Set this NestedInteger to hold a single integer.
* void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* void add(const NestedInteger &ni);
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* const vector<NestedInteger> &getList() const;
* };
*/
先在这说一下类的constructor的问题,也就是如何初始化一个类的对象。对于空的constructor,一般可以:
NestedInteger res;
NestedInteger res = NestedInteger();
这俩是等价的。注意不要随便用new,new代表在内存中新开一块地方,并把这个指针变量指向那块地方的首地址。
对于有参数的constructor,可以;
NestedInteger res(123);
NestedInteger res = NestedInteger(123);
这些都可以。
再来说说基于这个nested的类的几种关系,我一开始也觉得定义的有些混乱,所以我试了试几组输入和对应输出,体会了下这个类的一些关系。
举几个例子说明一下:
return NestInteger(123);
NestedInteger res;
res.add(NestedInteger(123));
return res;
第一个的输出是123,第二个的输出是[123],因为从constructor的定义来看,如果NestedInteger(123)只是在初始化一个这个类里可用的数,而add则是在hold a list,所以一旦使用add,就是在前后并列层面上去产生一个list,上例中的res虽然是空的,但这个list仍然成立,也就是这个list只要一个element,但是第一个里面就只是一个数,并没有list的概念,这个理解在算法的实施上很重要,因为你得知道是什么操作产生了分层或者并列。
下面给出递归和迭代两种做法:
方法一:递归
NestedInteger deserialize(string s) {
if (s.empty()) return NestedInteger();
if (s[0] != '[') return NestedInteger(stoi(s));
if (s.size() <= 2) return NestedInteger();
NestedInteger res;
int start = 1, cnt = 0;
for (int i = 1; i < s.size(); ++i) {
if (cnt == 0 && (s[i] == ',' || i == s.size() - 1)) {
res.add(deserialize(s.substr(start, i - start)));
start = i + 1;
} else if (s[i] == '[') ++cnt;
else if (s[i] == ']') --cnt;
}
return res;
}
因为题目说了输入都是规范的,所以我们将输入分成了只有数字的,和有括号的,并将只有数字的作为递归的base。cnt是代表第几层,不考虑内部的小循环,从整体来看,我们只需要在最初的这层使用add,保持在同一个层次并列这些元素,每次遇到‘,’或者最后一位‘]’,就代表一个元素的结束,add进并列即可。然后利用cnt加减来找出在第0层中的第一层甚至第二层,确定为一个并列整体后扔进递归,递归里的操作又会回到第0层来,所以我们只用保证始终在第0层操作就可以。
方法二:迭代
就好像我们之前遇到的很多一样,用递归做的一般可以考虑用stack来实现。(毕竟递归的本质也是使用内部函数里的栈)。这里其实跟之前的一题:decode string很像,大家可以对比看看。当我们用栈实现的时候,还是跟递归一样,注意好分层,因为这里的每一层我们都要在操作里留意,而不是递归那种只用关注一个层面,同时确定好base就行。
NestedInteger deserialize(string s) {
if (s.empty()) return NestedInteger();
if (s[0] != '[') return NestedInteger(stoi(s));
stack<NestedInteger> st;
int start = 1;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '[') {
st.push(NestedInteger());
start = i + 1;
} else if (s[i] == ',' || s[i] == ']') {
if (i > start) {
st.top().add(NestedInteger(stoi(s.substr(start, i - start))));
}
start = i + 1;
if (s[i] == ']') {
if (st.size() > 1) {
NestedInteger t = st.top(); st.pop();
st.top().add(t);
}
}
}
}
return st.top();
}
这里在迭代的过程中,当出现’]’的时候,大家注意到我们进行两次判断,第一次跟’,’一起,或的关系,第二次是判断栈内是否有超过一个的存储。这里很关键,因为这代表了’]’的两个方面的意义,首先,跟’,’一样,代表了当前的数字结束了,这个时候我们需要把这个结束的数字添加到当前层,同时,如果不是’,’而是’]’又代表了当前层的结束,所以如果此时栈内有两个以上的层结构存在,我们需要把当前层作为并列关系的一员添加到上一层中,然后对于接下来出现的数字,应该并列在上一层里,这样关系才对,大家体会一下。
对照例子理解:
[123, [234], 345];
[123, [234, 345]];